I'm performing validation on an entity property using a custom constraint, but my forms are using setters and getters that don't correspond directly to the validating field, but affect it.
What I want to do is hook the validation errors on to the form field that originally handled the invalid user input.
Here's a much simplified example of the validating property and the setter method that the form binds to:
/* @Assert\ValidFoo */
private $foo;
public function setFooBar( $baz ){
$this->foo->bar = $baz;
}
In my Twig template I want to be able to display the error with:
{{ form_errors(form.foobar) }}
I can dump out any errors at the end of the form with {{form_errors(form)}} but that's no good for my purposes. How can I alias the validation error onto the specific form field I want?
Update:
I've tried using addViolationAt as answered here but it's not working for me. Possibly because the property path doesn't really exist?
if (!callback) $form->get('yourField')->addError(new FormError('error message'));you can map the error callback directly to the field you want in your formType like this:$resolver->setDefaults(array('error_mapping' => array('yourCallback' => 'fieldYouWant')));