有没有办法在symfony2中向多个路径添加违规?

jbr*_*oxd 5 php constraints custom-validators symfony

是否可以向多个路径添加违规?喜欢:

$this->context->buildViolation($constraint->message)
                    ->atPath('initialDate')
                    ->atPath('finalDate')
                    ->addViolation();
Run Code Online (Sandbox Code Playgroud)

它只会增加initialDate.

Pie*_*NAY 4

您仍然可以添加两个违规行为,第二个违规行为为空消息

$this->context->buildViolation($constraint->message)
    ->atPath('phone')
    ->addViolation()
;
$this->context->buildViolation('')
    ->atPath('email')
    ->addViolation()
;
Run Code Online (Sandbox Code Playgroud)

但您也会在第二个字段中生成错误标记

多个领域被侵犯

如果没有消息,您还可以覆盖该form_errors块以调整标记

{% 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 %}
Run Code Online (Sandbox Code Playgroud)