Adding a field specific error from the controller in symfony2

假装没事ソ 提交于 2019-12-02 22:03:57
MrGlass

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;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!