我用过这个,它对我有用。
选项 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'
)
));