您必须扩展 form_validation,_field_data
属性受到保护,因此您无法在他的班级或他的子班级之外访问它。
$this->form_validation->set_message('required', 'some message')
也不会起作用,因为 CI 只会在他运行验证并且它与验证规则相关联时检查此数据。
库/MY_Form_validation.php
class MY_Form_validation extends CI_Form_validation {
public function __construct() {
parent::__construct();
}
public function add_message($field, $message) {
//this field was validated without error
if(isset($this->_field_data[$field]) AND
(!isset($this->_field_data[$field]['error']) OR !$this->_field_data[$field]['error']) )
$this->_field_data[$field]['error'] = $message;
}
}
$this->form_validation->add_message('field1', 'message for field1');
echo form_error('field1');
注意:
消息在 中不可用validation_errors()
。为此添加$this->_error_array[] = $message