0

在 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)
4

1 回答 1

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]);
于 2012-06-08T06:29:01.433 回答