Neeraj
Neeraj

Reputation: 9155

how to display all form error at one place

I have seen various posts regarding same issue but nothing could become useful for me.

This is my Form

<form action="" method="post" {{ form_enctype(form) }} novalidate>
    {{ form_errors(form) }}
    <div class="login_field">
        {{ form_label(form.name) }}
        {{ form_widget(form.name) }}                    
    </div>
    <div class="clear"></div>
    <div class="login_field">
        {{ form_label(form.status) }}
        {{ form_widget(form.status) }}                    
    </div>                
    <div class="login_field">
        <label>&nbsp;</label>
        <input type="submit" value="Create" class="submit_btn" />
    </div>
</form>

Error are not being displayed at all. How can i get out of this issue?

Upvotes: 6

Views: 2274

Answers (2)

Azam Alvi
Azam Alvi

Reputation: 7055

To display all the errors at top of form do like below

<ul>
    {% for error in form.vars.errors.form.getErrors(true) %}
        <li>{{ error.message }}</li>
    {% endfor %}
</ul>

Upvotes: 2

Nicolai Fr&#246;hlich
Nicolai Fr&#246;hlich

Reputation: 52493

You have to include form_errors for every field ...

<div class="login_field">
    {{ form_errors(form.name) }}    
    {{ form_label(form.name) }}
    {{ form_widget(form.name) }}                    
</div>

... or just use form_row to render all the three of them together ...

<div class="login_field">
    {{ form_row(form.name) }}                       
</div>

... or let your form errors bubble up to the top using the error_bubbling option for your form-fields in your FormType class. This means they will then be rendered through {{ form_errors(form) }}.

$builder->add('fieldname', 'text', array(
    'error_bubbling' => true,
));

quick tip: you can include the submit button in your FormType since symfony 2.3 and don't have to manually render it (reference).

Upvotes: 11

Related Questions