扩展表单验证 [英] Extend form validation
问题描述
我需要扩展表单验证库,以使一个方法返回错误数组。
I need extend the Form Validation library, to make a method to return the error array.
我在库中创建了一个带有'get_error_array'方法的新库文件夹:
I created a new library with the method 'get_error_array' within the library folder:
class My_Form_validation extends CI_Form_validation {
public function __construct(){
parent::__construct();
}
public function get_error_array(){
return $this->_error_array;
}
}
在自动加载配置文件中,我有:
In the autoload config file i have:
$autoload['libraries'] = array('form_validation', 'my_form_validation');
但是当我调用 $ this-> my_form_validation-> get_error_array )
But when i call $this->my_form_validation->get_error_array()
in a controller after validate a submit, the method return an empty array.
注意:验证是FALSE。
Note: the validation was FALSE.
有任何想法?
更新#1
根据文档更新我的代码,但结果是一样的:
According with the documentation i updated my code, but the result is the same:
class MY_Form_validation extends CI_Form_validation {
public function __construct(){
parent::__construct();
}
public function get_error_array(){
return $this->_error_array;
}
}
我禁用了自动加载,控制器:
I disabled the autoload, and load manually the library from the controller:
$this->load->library('form_validation');
/* Validation */
if($this->form_validation->run('join_request') === false){
$e = $this->form_validation->get_error_array();
var_dump($e);
}
http://www.codeigniter.com/user_guide/general/creating_libraries.html
UPDATE#2
我在构造函数和工作中添加了回显。在 get_error_array
中放置一个var_dump:
I put an echo on the constructor and work. In the get_error_array
put a var_dump:
# echo
extending CI_Form_validation ...
object(MY_Form_validation)[17]
protected 'CI' => & ...
protected '_field_data' =>
array (size=0)
empty
protected '_config_rules' =>
array (size=0)
empty
protected '_error_array' =>
array (size=0)
empty
protected '_error_messages' =>
array (size=0)
empty
protected '_error_prefix' => string '<p>' (length=3)
protected '_error_suffix' => string '</p>' (length=4)
protected 'error_string' => string '' (length=0)
protected '_safe_form_data' => boolean false
public 'validation_data' =>
array (size=0)
empty
这是我的验证文件config / form_validation.php):
This is my validation file (config/form_validation.php):
$config = array(
'radio_report' => array(
array(
'field' => 'id',
'label' => 'id',
'rules' => 'trim|required|is_natural_no_zero'
)
),
'join_request' => array(
array(
'field' => 'owner',
'label' => 'owner',
'rules' => 'trim|required|min_length[3]|max_length[100]|alpha'
),
array(
'field' => 'lang',
'label' => 'lang',
'rules' => 'trim|required|exact_length[2]|alpha'
),
array(
'field' => 'email',
'label' => 'email',
'rules' => 'trim|required|valid_email|max_length[60]'
),
array(
'field' => 'website',
'label' => 'website',
'rules' => 'trim|required|valid_url|max_length[255]'
),
array(
'field' => 'stream',
'label' => 'stream',
'rules' => 'trim|required|valid_url|max_length[255]'
),
array(
'field' => 'protocol',
'label' => 'protocol',
'rules' => 'trim|required|min_length[3]|max_length[30]'
),
array(
'field' => 'codec',
'label' => 'codec',
'rules' => 'trim|required|min_length[3]|max_length[10]'
)
)
);
推荐答案
我从子验证类中删除了结构,为我工作。
I removed the construct from the child validation class, and work for me !.
class MY_Form_validation extends CI_Form_validation {
public function get_error_array(){
return $this->_error_array;
}
}
这篇关于扩展表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!