I have a class
Class SubjectSegment{
/**
*@CustomAssert\MyCitizenshipNumber()
*/
private $citizenshipNumber;
/**
*@CustomAssert\MyDate()
*/
private $citizenshipNumberIssuedDate;
}
what i really want to do is valid citizenshipNumberIssuedDate if citizenshipNumber is present...
What is the best way to achieve this
Hi you should look on 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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With