1

I have a field with sets of validation rules:

/**
 * @var integer
 *
 * @ORM\Column(name="options", type="integer")
 * @Assert\NotBlank()
 * @Assert\Min(limit = "1381", message = "Please provide number higher than 1381")
 * @Assert\Regex(pattern = "/^\d{4}$/" , message = "Four digits are expected")
 */
private $options;

and it seems sometimes Symfony is checking every assertion(what is good) and render every error in view(what is not expected). After submitting a form with option=5 I got folowed error messages in view.

Options
- Please provide number higher than 1381
- Four digits are expected

but when I send option = null then only one error message is displayed.

Options
- This value should not be blank.

Is there a 'switch' which causes that only one error message is rendered in template? I would like to have only one error message per field in my forms.

====== edit ======
Solution I like by Bernhard Schussek
Patt thanks for pointing me there

3
  • 3
    A great way to do it would be to use validation sequencing as recommended by symfony form guru @Bernhard :) Commented Feb 10, 2013 at 2:29
  • This is the trick I needed - please type it as answer to let me up vote it. Thanks Commented Feb 10, 2013 at 22:34
  • 1
    Glad this helped, don't worry about the vite. This is @Bernhard answer. Maybe you could upvote his answer and edit your question so that people can have a link to Bernard answer :) Commented Feb 11, 2013 at 0:27

1 Answer 1

2

One option is to modify the template/block for a form error block application wide (so that every one of your forms in a template will get the desired behaviour). Or override the form layout on a per template basis.

If you go and look at the default form layout that ships with the Symfony standard edition you can see on line 273 the form_errors twig block is defined.

You could override this block in a given template (as per the documentation), to only echo one element from the errors array:

{# SomeTemplate:Default:index.html.twig #}
{% extends '::base.html.twig' %}

{% form_theme form _self %}

{% block form_errors %}
{% spaceless %}
    {% if errors|length > 0 %}
    {{ errors[0].message }}
    {% endif %}
{% endspaceless %}
{% endblock form_errors %}

{% block content %}
    {# ... render the form #}

    {{ form_rest(form) }}
{% endblock %}
Sign up to request clarification or add additional context in comments.

1 Comment

errors|length will be always 1 if error available or 0 if not. So even you have 2 errors (2 fields with errors) - you will get for field {{ id }} always count == 1. My proposition is to use JS and hide more than 1 errors

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.