Search code examples
formssymfonyrequired

Symfony 2.8 Form Required Field Error Text


I have a form field created using form builder. It looks like this:

    $builder
        ->add('firstName', null, array('required' => true, 'label' => 'First Name'))

Here is the full twig code for the form. I can't get the error message to display on fields when they blank on most of the fields. Email and password works fine but the other fields display no error message.

<div class="row">
    <div class="col-md-12">
        <div class="text-center">
            <h1>Create an Account</h1>
        </div>
        <div class="col-md-12">
            <div class="clear"></div>
        </div>
        <form id="loginForm" action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
            {# {{ form_widget(form) }} #}
            {{ form_errors(form) }}

            <div class="col-md-3">
                <fieldset>
                    <legend>Email</legend>
                        {{ form_row(form.email.children['first'], { 'label': 'Email Address', 'attr' : { 'class' : 'form-control' } }) }}
                        {{ form_row(form.email.children['second'], { 'label': 'Verify Email', 'attr' : { 'class' : 'form-control' } }) }}
                        {{ form_errors(form.email) }}
                        {{ form_errors(form.phone) }}
                        <div class="clear-small"></div>
                        <div class="clear-small"></div>
                         <legend>Password</legend>
                        {{ form_row(form.plainPassword.children['first'], { 'label': 'Choose a Password', 'attr' : { 'class' : 'form-control' } }) }}
                        {{ form_row(form.plainPassword.children['second'], { 'label': 'Verify Password', 'attr' : { 'class' : 'form-control' } }) }}
                        {{ form_errors(form.plainPassword) }}
                </fieldset>
                <div class="clear"></div>
            </div>
            <div class="col-md-3">
                <fieldset>
                    <legend>Contact Information</legend>
                    {{ form_errors(form.firstName) }}
                    {{ form_row(form.firstName, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.lastName, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.company, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.phone, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.fax, {'attr' : { 'class' : 'form-control' } }) }}
                </fieldset>
            </div>
            <div class="col-md-3">
                <fieldset>
                    <legend>Company Information</legend>                   
                    {{ form_row(form.company, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.address, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.address2, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.zip, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.city, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.state, {'attr' : { 'class' : 'form-control' } }) }}
                    {{ form_row(form.country, {'attr' : { 'class' : 'form-control' } }) }}

                </fieldset>
            </div>
            <div class="col-md-3">
                <legend>Shipping Information</legend>      
                {{ form_row(form.ship_company, {'attr' : { 'class' : 'form-control' } }) }}
                {{ form_row(form.ship_contact, {'attr' : { 'class' : 'form-control' } }) }}
                {{ form_row(form.ship_address, {'attr' : { 'class' : 'form-control' } }) }}
                {{ form_row(form.ship_address2, {'attr' : { 'class' : 'form-control' } }) }}
                {{ form_row(form.ship_zip, {'attr' : { 'class' : 'form-control' } }) }}
                {{ form_row(form.ship_city, {'attr' : { 'class' : 'form-control' } }) }}
                {{ form_row(form.ship_state, {'attr' : { 'class' : 'form-control' } }) }}
                {{ form_row(form.ship_country, {'attr' : { 'class' : 'form-control' } }) }}             
                {{ form_row(form.ship_phone, {'attr' : { 'class' : 'form-control' } }) }}             
            </div>

            <div class="clear"></div>

            <div>
                {{ form_widget(form._token) }}
                <input class="dark-blue-bg white-text btn" type="submit" id="_submit" name="_submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}"/>
            </div>

        </form>
        <div class="clear"></div>
    </div>
</div>

When the user hits register without filling out first name they get no error text. How would I go about fixing that?

Thanks!


Solution

  • Try using form_start

    {{ form_start(form, {id: "loginForm", 'method': 'POST', action: path('fos_user_registration_register'), class:"fos_user_registration_register" }) }}
    

    and remove

    <form id="loginForm" action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
    

    end close with form_end