【问题标题】:symfony2 form validation radio buttonssymfony2 表单验证单选按钮
【发布时间】:2013-12-04 07:45:14
【问题描述】:

我正在使用此代码创建 symfony2 表单:

$builder
        ->add('layout_type', 'choice', array(
                'choices'   => array(0 => 'Small layout', 1 => 'Big layout'),
                'expanded' => true,
                'disabled' => $disabled
            ))

.... ))

我遇到的问题是单选按钮验证(layout_type、choice)。

当我没有选择任何单选按钮并提交表单时,数据不会插入到数据库中。 这是正确的,因为单选按钮是必填字段,但是当重新加载表单时,我没有关于单选按钮错误的消息。我正确收到所有其他字段的错误。

我正在使用的树枝:

<table width="1000">
        <tr>
            <td class="label">{{ form_label(form.layout_type, 'LAYOUT_TYPE') }}</td>
            <td class="widget">{{ form_widget(form.layout_type) }} {{ form_errors(form.layout_type) }}</td>
        </tr>
        ...

为了验证我正在使用注释:

 /**
 * @var Integer
 * @Assert\NotBlank(message="NOT_EMPTY")
 */
protected $layoutType;

我只有 $layoutType 单选按钮的错误消息有问题 有人知道可能是什么问题。 谢谢

【问题讨论】:

    标签: forms validation symfony radio-button formbuilder


    【解决方案1】:
    try this:
    
    'choices'   => array(0 => 'Small layout', 1 => 'Big layout'),
                        'expanded' => true,
                        'disabled' => $disabled,
                        'constraints' => new Assert\NotBlank(array('message' => 'YOUR_MESSAGE'))
    

    【讨论】:

    • 是的,它正在工作。谢谢。但我还是不明白为什么注释不起作用。
    【解决方案2】:

    你添加了吗

    use Symfony\Component\Validator\Constraints as Assert;
    

    在你的实际实体之前

    class entity {
    
        /**
         * @Assert\NotBlank()
         */
        public $layoutType;
         ........
    }
    

    【讨论】:

    • 是的,我做到了。对于其他字段,错误消息正常工作。
    • 添加assert约束后,是否重新生成了实体?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2012-12-01
    相关资源
    最近更新 更多