1
votes

I use Doctrine 2 and Symfony 2 Validator Component (standalone, without Forms Component).

So, when I finish checking the Doctrine Entity and pass it to SF2 Validator, I need to add custom error message to the validator. How can I do that?

This is my code so far:

$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
$errors = $validator->validate($entry);

// Add custom error will be here
$errors->add(new ConstraintViolation("Error text maybe here"));
1

1 Answers

3
votes

It should looks like this:

$error = new ConstraintViolation('Error message', '', [], $entity, 'fieldName', 'value that caused this violation');
$errors->add($error);