I have a custom validator where I'm getting the value of one of the fields defined in my entity, and then I want to validate that field using built in validator (ex. NotBlank()) so that after a validation I get 'true' in case the field is validated and 'false' if it's not.
My custom validator looks like this:
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ConstraintValidator;
class UpdatePasswordValidator extends ConstraintValidator
{
public function __construct()
{
}
public function validate($email, Constraint $constraint)
{
/**
* Getting field value from entity
*/
$busNumber = $this->context->getRoot()->getData()->getBusnumber();
/**
* Validate $busNumber
*/
$busNumberToValidate = $this->context->getValidator()
->inContext($this->context)
->atPath("busnumber")
->validate($busNumber, new NotBlank())->getViolations();
}
}
In this case I want to validate $busNumber using NotBlank(). Calling getViolations() gives me an object with all violations while I just need one that is associated with the $busNumber validation.
Update: what I'm actually trying to achieve is this:
if ($busNumberToValidate)
{
echo "busNumber field is validated";
}
else
{
echo "busNumber field is NOT validated";
}
The $busNumberToValidate should contain or not an error for 'busNumber' field depending on the validation result.