Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 217
neeraj Avatar asked Sep 17 '13 11:09

neeraj


2 Answers

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).

like image 56
Nicolai Fröhlich Avatar answered Oct 16 '22 08:10

Nicolai Fröhlich


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>
like image 42
Azam Alvi Avatar answered Oct 16 '22 08:10

Azam Alvi