我有一节课

Class SubjectSegment{
 /**
 *@CustomAssert\MyCitizenshipNumber()
 */
 private $citizenshipNumber;
 /**
 *@CustomAssert\MyDate()
 */
 private $citizenshipNumberIssuedDate;
}

我真正想做的是有效的公民号如果公民号存在…
实现这一目标的最佳方法是什么

最佳答案

嗨,你应该看看http://symfony.com/doc/current/reference/constraints/Callback.html

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContext;

/**
 * @Assert\Callback(methods={"isCitizenDateValid"})
 */

Class SubjectSegment{
    /**
    *@CustomAssert\MyCitizenshipNumber()
    */
    private $citizenshipNumber;
    /**
    *@CustomAssert\MyDate()
    */
    private $citizenshipNumberIssuedDate;

    public function isCitizenDateValid(ExecutionContext $context)
    {
        //Do your validation here for your exemple :
        if(null === $this->citizenshipNumber && null === $this->citizenshipNumberIssuedDate) {
            $context->addViolationAtSubPath('citizenshipNumberIssuedDate', 'The date is required', array(), null);
        }
    }
}

09-10 13:27