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

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

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

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

它只会添加到 initialDate

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

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

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

如果没有消息,您也可以覆盖 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 %}