1

如果未选中任何按钮,我想更改单选按钮组的错误消息文本。目前我正在尝试这样:

    $this->add(
        array(
            'name' => 'target_code',
            'validators' => array(
                array(
                    'name' => 'NotEmpty',
                    'options' => array(
                        'messages' => array(
                            \Zend\Validator\NotEmpty::IS_EMPTY => 'My Custom Error Message.'
                        )
                    )
                )
            )
        ));

这行不通。我究竟做错了什么?

4

2 回答 2

2

我用过这个,它对我有用。

选项 1:更改错误消息

形式:

$this->add(array(
    'type' => 'Radio',
    'name' => 'gender',
    'options' => array(
        'label' => 'Select your gender',
        'value_options' => array(
            1 => 'Male',
            2 => 'Female',
        ),
    )
));

表单过滤器:

$this->inputFilter->add($factory->createInput(array(
            'name' => 'gender',
            'required' => true,
            'validators' => array(
                array(
                    'name' => 'InArray',
                    'break_chain_on_failure' => true,
                    'options' => array(
                        'haystack' => array(1, 2),
                        'messages' => array(
                            \Zend\Validator\InArray::NOT_IN_ARRAY => 'Please select your gender!'
                        ),
                    ),
                ),
                array(
                    'name' => 'NotEmpty',
                    'break_chain_on_failure' => true,
                    'options' => array(
                        'messages' => array(
                            \Zend\Validator\NotEmpty::IS_EMPTY => 'Please select your gender!',
                        ),
                    ),
                ),
            ),
)));

查看错误:

foreach ($form->get('gender')->getMessages() as $message) {
    echo $message;
    break;
}

问题:

'break_chain_on_failure' => true

“break_chain_on_failure”对我不起作用,所以我显示了第一条错误消息:)

选项 2:将一项设置为始终选中

$this->add(array(
    'type' => 'Radio',
    'name' => 'gender',
    'options' => array(
        'label' => 'Select your gender',
        'value_options' => array(
            1 => 'Male',
            2 => 'Female',
        ),
    ),
    'attributes' => array(
        'value' => '1' //set checked to '1'
    )
));
于 2013-05-14T09:57:48.763 回答
1

这个链接可能有用。我不明白原因,但它说:“您必须将 required 设置为 false 并将 allow_empty 设置为 true 才能在 NotEmpty 验证器上设置自定义消息。”

于 2013-01-21T18:22:17.893 回答