I have a simple form in Symfony 2.1 and when I submit it, I expect to see some error messages displayed on my form page because I denoted some fields as required.
However, the {{ form_errors(form.company_name) }} approach to show the error message of that particular field doesn’t work for me. It never displays the message. However, if I use {{form_errors(form)}}, all the error messages are displayed in one place. I need the individual {{form_errors(form.company_name)}} way of doing it to work. Did anybody experience this? What could be wrong? Here is my twig code:
<form method="post" {{ form_enctype(form) }} novalidate>
<table>
<tr>
<td class="field-cell">{{ form_label(form.promo_referral) }}: </td>
<td>{{ form_errors(form.promo_referral) }}{{ form_widget(form.promo_referral) }}</td>
</tr>
<tr>
<td class="field-cell">{{ form_label(form.company_name) }}: </td>
<td>{{ form_errors(form.company_name) }}{{ form_widget(form.company_name) }}</td>
</tr>
...
Here is my relevant Controller code:
$registration = new ServiceVendorRegistration();
$form = $this->createForm(new ServiceRegisterForm(), $registration);
if ($request->isMethod("POST"))
{
$form->bind($request);
if ($form->isValid())
{
// do something else
}
}
return $this->render('MyBundle:Default:register_service_vendor.html.twig', array('form' => $form->createView()));
Validation.yml makes the field required as follows:
properties:
companyName:
- NotBlank:
message: You have to fill this
Any help would be greatly appreciated.
I got the solution. Symfony doesn’t like it when I name my form fields using underscore to separate words, like first_name. It relies on camelCase naming convention. When I renamed my form fields correspondingly, it started working right.