Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to add violation to multiple paths in symfony2?

is it possible to add a violation to multiple paths? like:

$this->context->buildViolation($constraint->message)
                    ->atPath('initialDate')
                    ->atPath('finalDate')
                    ->addViolation();

it only add to initialDate.

like image 302
jbrunoxd Avatar asked Sep 26 '22 23:09

jbrunoxd


1 Answers

You can still add two violations with an empty message on the second

$this->context->buildViolation($constraint->message)
    ->atPath('phone')
    ->addViolation()
;
$this->context->buildViolation('')
    ->atPath('email')
    ->addViolation()
;

but you will have the error markup generated also in the second field

Multiple fields violated

You can also override the form_errors block to adjust the markup if no message

{% block form_errors -%}
    {% if errors|length > 0 -%}
    {% if form.parent %}<span class="help-block">
    {% else %}<div class="alert alert-danger">{% endif %}
    <ul class="list-unstyled text-danger">
        {%- for error in errors if error.message -%}
            <li><span class="glyphicon glyphicon-exclamation-sign"></span>
                {{ error.message }}
            </li>
        {%- endfor -%}
    </ul>
    {% if form.parent %}</span>{% else %}</div>{% endif %}
    {%- endif %}
{%- endblock form_errors %}
like image 60
Pierre de LESPINAY Avatar answered Nov 02 '22 04:11

Pierre de LESPINAY