1

How do I access and use validators inside an Entity?

A little detail on what I'm trying to do:

I have a User Entity and want to create a validation rule that says: If Users hair color is set and is valid, eye color is not mandatory, else eye color is mandatory.

So in my validation.yml I've set a callback validator and indicated that I want to call a funciton: isHairOrEyeValid(). This funciton is defined in the User entity. I need to use some of the symfony2 built in validators inside the isHairOrEyeValid() function of the User Entity.

1 Answer 1

3

You can use most validators by instantiating them inline:

$validator = new EmailValidator();
$valid = $validator->isValid($email, new Email());

In your case, it looks like you can accomplish this validation with simple PHP. I'm not sure why you want to involve validators.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.