在Codeigniter 2中,有一种方法可以在下面的数组中自定义验证消息,而不是 $ this-> form_validation-> set_message
$ValidationRules = array(
array('field' => 'address_line1',
'label' => 'Address Line1',
'rules' => 'trim|min_length[2]|max_length[40]|xss_clean'),
array('field' => 'address_line2',
'label' => 'Address Line2',
'rules' => 'trim|min_length[2]|max_length[40]|xss_clean'),
array('field' => 'address_line3',
'label' => 'City',
'rules' => 'trim|min_length[2]|max_length[40]|xss_clean')
);
$this->form_validation->set_rules($ValidationRules);
if ($this->form_validation->run() == FALSE)
答案 0 :(得分:0)
如果您希望为每条规则发送消息,则可以创建一个新规则作为密钥。
$validationMessages = array('min_length' => 'String not long enough', 'max_length' => 'String too long');
$this->addMessage($field, $validationMessages[$key]);
'Field'表示发生验证错误的字段,'key'是出现问题的特定业务规则。您可以通过向消息添加自定义变量来扩展它,以显示更准确的消息:
$validationMessages = array('min_length' => '{field} not long enough', 'max_length' => '{field} too long');
$this->addMessage($field, str_replace('{field}', $field['label'], $validationMessages[$key]);