在表单元素中,我编辑了错误消息:
$this->addElement('text', 'type', array(
'label' => 'Type: ',
'class' => 'form-control',
'placeholder' => 'type',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array('min' => 3, 'max' => 10, 'messages' => array('stringLengthTooShort' => 'The type is too short.', 'stringLengthTooLong' => 'The type is too long.')))
,array('validator' => 'NotEmpty', 'options' => array('messages' => array('isEmpty' => 'Type is required.')))
),
));
问题是,如果我发送空表单,则会显示两条消息(类型太短+需要类型。)。但在这种情况下,我只需要显示NotEmpty错误消息而不是Stringlength(min length 3)错误消息。所以我需要在NotEmpty验证后验证minlength。感谢
修改: 我已经尝试改变调用required和validators的不同位置以及它们的链的不同设置true / false(下一个代码),但是如果元素为空,它仍然显示两个错误消息
$this->addElement('text', 'type', array(
'label' => 'Type: ',
'class' => 'form-control',
'placeholder' => 'type',
'validators' => array(
array('validator' => 'NotEmpty',true, 'options' => array('messages' => array('isEmpty' => 'Type is requiredDDDD.'))),
array('validator' => 'StringLength',false, 'options' => array('min' => 3, 'max' => 10, 'messages' => array('stringLengthTooShort' => 'The type is too short.', 'stringLengthTooLong' => 'The type is too long.')))
),
'required' => true,
));
答案 0 :(得分:1)
您可以尝试这样的事情:
评论第二个验证器
$this->addElement('text', 'type', array(
'label' => 'Type: ',
'class' => 'form-control',
'placeholder' => 'type',
'required' => true,
'validators' => array(
//array('validator' => 'StringLength', 'options' => array('min' => 3, 'max' => 10, 'messages' => array('stringLengthTooShort' => 'The type is too short.', 'stringLengthTooLong' => 'The type is too long.'))),
array('validator' => 'NotEmpty', 'options' => array('messages' => array('isEmpty' => 'Type is required.')))
),
));
并在isValid()
函数中添加验证器:
public function isValid($data) {
$etat_valid = parent::isValid($data);
$error = $this->getElement('type')->getMessages();
if (empty($error)){
$validator = new Zend_Validate_StringLength(array(
'min' => 3,
'max' => 10,
'messages' => array('stringLengthTooShort' => 'The type is too short.',
'stringLengthTooLong' => 'The type is too long.')
)
);
$etat_valid_type = $validator->isValid($data['type']);
if (!$etat_valid_type){
$this->getElement('type')->addErrors($validator->getMessages());
}
}
return $etat_valid && $etat_valid_type;
}
答案 1 :(得分:1)
我上次评论的解决方案,如何为同一表格中的更多元素添加非空+不同的字符串长度验证:
public function isValid($data) {
$etat_valid = parent::isValid($data);
$elements = Array(Array('name', 4, 9), Array('type', 3, 10), Array('type2', 2, 12));
for ($index = 0; $index < count($elements); $index++) {
$error = $this->getElement($elements[$index][0])->getMessages();
if (empty($error)) {
$validator = new Zend_Validate_StringLength(array(
'min' => $elements[$index][1],
'max' => $elements[$index][2],
)
);
$validator->setMessages(array(
Zend_Validate_StringLength::TOO_SHORT =>
'Too short: min '.$elements[$index][1].' chars.',
Zend_Validate_StringLength::TOO_LONG =>
'Too long: max '.$elements[$index][2].' chars.'
));
$etat_valid_type = $validator->isValid($data[$elements[$index][0]]);
if (!$etat_valid_type) {
$this->getElement($elements[$index][0])->addErrors($validator->getMessages());
}
}
}
return $etat_valid && $etat_valid_type;
}
所以thx为你的答案Doydoy