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

标签 php symfony constraints custom-validators

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

$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()
;

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

Multiple fields violated

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

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

关于php - 有没有办法在 symfony2 中向多个路径添加违规?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34230698/

相关文章:

php - 替换 "vendor"目录后使用 Twig 时出现权限被拒绝错误

grails域类验证器+根据字段值设置唯一约束?

php - 缓存不工作

php - 为什么 Symfony 表单不使用约束注释验证我的 DTO?

php - Laravel 4 中 Request::route() 的等价物是什么?

symfony - 可以逐字逐句地用于包含的内容吗?

algorithm - 存在约束下的排列(Interview Street - Manipulative Numbers)

sql - 数据库表设计-邮件类型的不同状态

php - 基于地区的唯一代码

php - 持久连接不适用于 codeIgniter 中的 mysql 驱动程序