I want to validate an email entity using @Assert\NotBlank and @Assert\Email, only if checkbox is checked ($updateEmailCheckBox). Because now those asserts are defined in annotation of email field and the validation occurs also when the checkbox is not checked.
Here's the snippet of my User enity:
/**
* Update email checkbox entity
*/
private $updateEmailCheckBox;
/**
* @var string
* @Assert\NotBlank(message="Email can't be empty")
* @Assert\Email(message="Wrong email pattern")
*/
private $email;
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
/**
* If update email checkbox is checked
*/
if ($this->getUpdateEmailCheckBox() == 1)
{
// how to validate here an email field for Assert\NotBlank and Assert\Email ?
}
}
I can check whether checkbox is checked or not using callback, but I can't figure out how to enable NotBlank and Email validations, and in case of an constraint violation return corresponding errors.