dspym82000 2013-04-12 13:16 采纳率: 100%
浏览 36
已采纳

Symfony2验证日期时间1应该在日期时间2之前

I'm looking through the Symfony2 Validation Reference but I'm not finding what I need.

I have a class Employment with a StartDate and EndDate. I would like to add an \@Assert() where it verifies that StartDate is always BEFORE EndDate. Is there a standard way of comparing class attributes as a Validation Constraint or should I create a custom validation constraint?

class Employment {

    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    * @Expose() 
    */
    protected $id;

    /**
    * @ORM\Column(type="datetime") 
    * @Expose()
    * @Assert\DateTime()
    */
    protected $startDate;

    /**
    * @ORM\Column(type="datetime", nullable=TRUE)
    * @Expose()
    * @Assert\DateTime()
    */
    protected $endDate;

...
}
  • 写回答

3条回答 默认 最新

  • douzheyo2617 2013-04-12 13:26
    关注

    You could write a custom DateRangeValidator.

    class DateRange extends Constraint {
    
        public $message = "daterange.violation.crossing";
        public $emptyStartDate = "daterange.violation.startDate";
        public $emptyEndDate = "daterange.violation.endDate";
    
        public $hasEndDate = true;
    
        public function getTargets() {
            return self::CLASS_CONSTRAINT;
        }
    
        public function validatedBy() {
            return 'daterange_validator';
        }
    }
    
    class DateRangeValidator extends ConstraintValidator
    {
    
        public function isValid($entity, Constraint $constraint)
        {
    
            $hasEndDate = true;
            if ($constraint->hasEndDate !== null) {
                $hasEndDate = $constraint->hasEndDate;
            }
    
            if ($entity->getStartDate() !== null) {
                if ($hasEndDate) {
                    if ($entity->getEndDate() !== null) {
                        if ($entity->getStartDate() > $entity->getEndDate()) {
                            $this->setMessage($constraint->message);
                            return false;
                        }
                        return true;
                    } else {
                        $this->setMessage($constraint->emptyEndDate);
                        return false;
                    }
                } else {
                    if ($entity->getEndDate() !== null) {
                        if ($entity->getStartDate() > $entity->getEndDate()) {
                            $this->setMessage($constraint->message);
                            return false;
                        }
                    }
                    return true;
                }
            } else {
                $this->setMessage($constraint->emptyStartDate);
                return false;
            }
        }
    

    register it as a service:

    parameters:
        register.daterange.validator.class:     XXX\FormExtensionsBundle\Validator\Constraints\DateRangeValidator
    
    services:
        daterange.validator:
            class: %register.daterange.validator.class%
            tags:
                - { name: validator.constraint_validator, alias: daterange_validator }
    

    And use it in your Entity:

    use XXX\FormExtensionsBundle\Validator\Constraints as FormAssert;
    
    /**
     *
     * @FormAssert\DateRange()
     */
    class Contact extends Entity
    {
        private $startDate;
    
        private $endDate;
    }
    

    Eventhough it seems a bit much for a simple thing like that, but experience shows, that one needs a date range validator more often than just once.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器