Adding a field specific error from the controller in symfony2 Adding a field specific error from the controller in symfony2 symfony symfony

Adding a field specific error from the controller in symfony2


Thanks to some help over IRC (thanks @fkrauthan!) I came up with an answer.

Every field in SF2 is actually an instance of form. What you need to do is access the form object of the field, and add then error onto it. Thankfully, symfony provides a method to get an embedded form/field.

Heres my code:

$error = new FormError("There is an error with the field");$form->get('field')->addError($error);

As some people have pointed out, you will need to include the FormError class at the top of your file:use Symfony\Component\Form\FormError;