66

In Twig template I check if a field has an error like this:

{% if form.points.get('errors') is not empty %}

Is there any method like:

{% if form.points.hasErrors() %}

to do it simpler? It's not a big difference, but if I can't do it easier why not.

Lars Strojny
  • 667
  • 4
  • 11
Dawid Ohia
  • 16,129
  • 24
  • 81
  • 95
  • Many answers have been added and some of them depend on the `error_bubbling` setting. I've added a PR to the symfony cookbook to improve the docs. Feel free to vote for it here: https://github.com/symfony/symfony-docs/issues/6145 – webDEVILopers Jan 14 '16 at 10:39
  • See my comment about how to do it with symfony 3.4, the dirty way through twig for&set or inside the controller : https://stackoverflow.com/a/49785276/7491491 Don't forget to upvote if it fix your needs :) – Raphaël G. Apr 11 '18 at 22:47

10 Answers10

119

better way I found, is to use this kind of code

{% if not form.vars.valid %}
<div class="alert alert-error">
    {{ form_errors(form) }}
</div>
{% endif %}
birko
  • 1,356
  • 1
  • 9
  • 13
  • 1
    Sadly this is not working for me, while calling `{{ form_errors(form.somefield) }}` still works. – ex3v May 25 '14 at 10:02
  • 2
    @ex3v it depends on how you have set your error bubbling – birko May 28 '14 at 06:28
  • 1
    could you expand your answer? – ex3v May 28 '14 at 13:32
  • 3
    @ex3v as described here http://symfony.com/doc/current/reference/forms/types/form.html#error-bubbling default behavior has it set to be all errors displayed by the valid field. using form_row(form.field) or form_errors(form.field) wil display them. if you set error_bubbling true for that field. the error will be displayed in his parent form. – birko May 29 '14 at 07:30
  • Thanks! But things are getting complicated when you already have, say, 100 forms. I'm writing bundle to help resolve that, gonna share when it's done. – ex3v May 29 '14 at 09:45
  • This worked for me when i used: {% if form.vars.valid == false %} – Simon Epskamp Feb 06 '15 at 09:26
  • Note that in general, before testing the validity status you should also check if the form was submitted (like you would do in a controller), this can be done the same way with: `{% if form.vars.submitted %}` – COil Nov 08 '19 at 06:11
94

That method does not exist. I typically do {% if form.points.vars.errors|length %}.

Kris Wallsmith
  • 8,945
  • 1
  • 37
  • 25
18

You can also check for errors when overriding field rendering:

{% block field_row %}
{% spaceless %}    
    <div class="control-group {% if errors %}error{% endif %}">
      {{ form_label(form) }}
      <div class="controls">
        {{ form_widget(form) }}        
        {{ form_errors(form) }}        
      </div>
    </div>    
{% endspaceless %}
{% endblock field_row %}
jkucharovic
  • 4,214
  • 1
  • 31
  • 46
14

For deeper form customization I do:

<div class="form-group {% if form.MYFORMINPUT.vars.valid==false %}has-error{% endif %}">
//some twisted divs
{{form_label(form.MYFORMINPUT)}}
{{form_widget(form.MYFORMINPUT)}}
</div>

Sf2.5

d3uter
  • 737
  • 9
  • 14
4

If you are using symfony version >= 4, you can check errors existence with this code

{% if form_errors(registrationForm) %}
    <div class="alert alert-danger">        
        {{ form_errors(registrationForm) }}
    </div>
{% endif %}
Ebenezer Nikabou
  • 369
  • 4
  • 14
1

This is what i use :

 <div class="form-group {{ form.brand.vars.errors|length > '' ? 'has-error' }}">
mrDjouk
  • 271
  • 5
  • 8
1

The simplest way of checking whether the form has an error:

{% if not form.vars.errors|length %}

{% endif %}

Symfony version >= 4

0

Since an empty array resolves to false, you can shorten your existing check to

{% if form.WIDGET_NAME.get('errors') %}
Riccardo Galli
  • 12,419
  • 6
  • 64
  • 62
-2

i have create a twig extension to handle this: my extension

public function hasError($string)
{
    if(strlen($string) > 4)
        return true;
    return false;
}

i use it like this in twig

{{ has_error(form_errors(form.username)) ? form_errors(form.username) : '' }}
Belga
  • 1
  • 1
-2

I had a similar problem, but form.points doesn't exist in my twig templates.

I had to get the number of errors in the controller, then pass it into my templates as a variable. $form->getErrors() does not behave as you might expect in your controller though. See this SO question for a function that will get the form errors correctly.

Community
  • 1
  • 1
adavea
  • 1,535
  • 1
  • 19
  • 25