2

Using Symfony2.3.4 and Twig I bet it's an easy peasy for anybody but I just can't get it done.
I'm trying to show only one error alert with always the same message, always at the beginnig of the form, everytime there are errors regardless which field contains the invalid input.

I thought I could do it like this:

//../Professor/Resources/new.html.twig
    {% extends 'AdminBundle:Default:admin.html.twig' %}

{% block content -%}
<h1>New professor</h1>
{% if form_errors(form) is not null %}  {#<------------------------------------#}
<div class="alert alert-error">
    <i class="glyphicon-ban-circle"></i>&nbsp;Message.
</div>
{% endif %}

<div class="row-fluid">      
    <form id="prof_create" class="form-horizontal sf_admin_form_area" action="{{ path('professor_create') }}" 
          method="post" {{ form_enctype(form) }}>
            {{ form_widget(form) }}    
        <div class="row-fluid">
            <div class="span8"></div>    
        </div>
        <div class="form-actions">
            <button class="btn btn-primary">
                <i class="icon-ok"></i> {{'Create' | trans}}</button>

            <a class="btn" href="{{ path('professor') }}">
                <i class="icon-ban-circle"></i> {{'Cancel' | trans }}</a>
        </div>    
    </form>
</div>
{% endblock %}

{% if form_errors(form) is not null %} is not working, meaning:
when I show the create form for the first time before entering any data in the fields,the error alert shows although there is no data in the fields.

I also tried {% if form_errors(form) %} which is also useless but the other way around, it doesn't matter if there are or not errors, the alert just won't show.

There's obviously a lot about form_errors(form) that I don't know.
Appreciate any tips or even completely different solutions.
Thanks

1
  • 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: github.com/symfony/symfony-docs/issues/6145 Commented Jan 14, 2016 at 10:39

4 Answers 4

4

Have you try this ?

{% if form_errors(form.content) is not empty %}
Sign up to request clarification or add additional context in comments.

Comments

1

try:

{% if form_errors(form) != '' %}
...
{% endif %}

You will also need to make sure that the error_bubbling option on all of your fields is set to true. If you dont the error only exists on the child. So the main form wouldnt have any errors even though some of the children do.

To set the error bubbling you need to do it to each field:

//Some FormType that you have created
class CustomFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('test', 'text', array('error_bubbling'=>true);
    }
}

Your other option is to override the form_errors block like suggested in the accepted answer here:

{% block form_errors %}
    {% spaceless %}
        {% set a = false %}
        {% for child in form.children  %}
            {% if child.get("errors") %}
                {% set a = 'true' %}
            {% endif %}
        {% endfor %}
        {% if a == true %}
            <div class="alert">
                {% for children in form.children %}
                    {{  form_errors(children) }}
                {% endfor %}
            </div>
        {% endif %}
        {% if errors|length > 0 %}
            <ul>
                {% for error in errors %}
                    {{
                    error.messagePluralization is null
                    ? error.messageTemplate|trans(error.messageParameters, 'validators')
                    : error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
                    }}
                {% endfor %}
            </ul>
        {% endif %}
    {% endspaceless %}
{% endblock form_errors %}

5 Comments

I tried what you just posted, I'm sorry but !== triggers this error "Unexpected token "operator" of value "="...". Then I tried != and it doesn't show errors but doesn't work: the alert doesn't show having errors in the inputs, back to the beginning...
{{dump(form_errors(form))}} shows 'string '' (length=0)' and there ARE errors in the inputs, I really don't get it
How can I set mine to true too? sorry I've only been doing web programming for a few months now.
I've updated my answer again to show how to set the error bubbling on a field
I tried the second option and used the code you posted with some small tweaks,i.e: child.get(errors) throws "Method "get" for object "Symfony\Component\Form\FormView" does not exist" and I replaced it with form_errors(child),there is definitely something weird with the symfony I'm using, anyways in the end it worked as I needed, thanks a million, if (myRep >= 15) $me->thumbsUp();
1

try

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

Comments

1
{% if not form.vars.valid %}
<div class="alert alert-error">
    {{ form_errors(form) }}
</div>
{% endif %}

from https://stackoverflow.com/a/17826389/511514

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.