在验证symfony选择表格时正确使用“min”

时间:2014-08-07 15:40:50

标签: forms validation symfony multiple-choice

如书http://symfony.com/doc/current/reference/constraints/Choice.html#min中所述 我想使用" min"验证选项的选项必须至少包含一个选中的复选框

表单看起来像

->add('usergroups', 'entity', array('class' => 'PrUserBundle:Group','property' => 'name','required' => true, 'multiple'  => true, 'expanded'  => true))

我的validation.yml看起来像谎言:

Pr\UserBundle\Entity\User:
constraints:
//...
properties:
    //.....
    locations:
        - Length:
            min: 7 { message: "Please select at least one group." }

我这样做是完全错误的,但这本书不会告诉我任何更多帮助,所以我问你。 或者我必须使用真/假吗?

1 个答案:

答案 0 :(得分:1)

您正在尝试使用“长度”约束而不是选择。

你应该像...一样使用它。

Pr\UserBundle\Entity\User:
    constraints:
    //...
    properties:
        //.....
        locations:
            - Choice:
                min: 7
                minMessage: "Please select at least one group."

该页面上显示的所有选项都将位于单个级别数组中,如

- Choice: { min: 7, minMessage: 'message' }

- Choice:
    min: 7
    minMessage: 'message'