Search code examples
phpsymfonyconstraintscustom-validators

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.


Solution

  • 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 %}