Php 有没有办法在symfony2中向多个路径添加冲突?
是否可以将冲突添加到多个路径?比如:Php 有没有办法在symfony2中向多个路径添加冲突?,php,symfony,constraints,custom-validators,Php,Symfony,Constraints,Custom Validators,是否可以将冲突添加到多个路径?比如: $this->context->buildViolation($constraint->message) ->atPath('initialDate') ->atPath('finalDate') ->addViolation(); 它只会添加到initialDate您仍然可以添加两个违规行为,第二个违
$this->context->buildViolation($constraint->message)
->atPath('initialDate')
->atPath('finalDate')
->addViolation();
它只会添加到
initialDate
您仍然可以添加两个违规行为,第二个违规行为上显示一条空消息
$this->context->build违例($constraint->message)
->atPath(“电话”)
->addViolation()
;
$this->context->buildViolation(“”)
->atPath(“电子邮件”)
->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 %}
{%block form_errors-%}
{%如果错误|长度>0-%}
{%if form.parent%}
{%else%}{%endif%}
{%-for error in errors if error.message-%}
-
{{error.message}}
{%-endfor-%}
{%if form.parent%}{%else%}{%endif%}
{%-endif%}
{%-endblock form_errors%}
否,您需要为不同的冲突路径构建两次不幸的是,构建冲突需要一条消息。即使传递了空字符串,这也会给path中定义的表单字段添加错误。标签将为红色,并且会出现带有感叹号且没有消息的错误。删除表单错误也将删除标记为红色的标签。似乎没有解决办法。通过实验,我确认这在Drupal 8(基于Symfony 3)中也有效,并且空消息的表现形式也类似:当所有消息同时输出时,在实际消息堆栈中几乎没有明显的空消息,但字段按预期/所需被很好地突出显示。