验证来自Codeigniter 3中自定义库的表单输入



我有一个网站,其中有多个具有相同规则集的表单验证过程。所以我想把验证代码移到一个单独的库文件中。但当我测试它时,验证总是失败。。为什么?

这是我的代码:

库文件:

<?php
/*
* filename: My_validations.php
* filedir: application/libraries/
*/
defined('BASEPATH') or exit('No direct script access allowed');
class My_validations
{
public function __construct()
{
$this->ci = &get_instance();
}
public function get_rules($params=[])
{
$rules = [];
$module = $params['module'];
if ($module == 'test')
{
$rules[] = [
'field' => 'phone',
'label' => 'test phone 1',
'rules' => 'callback__test_rule1'
];
}
return $rules;
}
// end public function get_rules
public function _test_rule1()
{
return true;
}
}

控制器文件:

<?php
/*
* filename: test1.php
* filedir: application/modules/test/controllers/
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends MY_Controller 
{
public function test1()
{
$this->load->library('form_validation');
$this->load->library('My_validations');
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
$params = [
'module' => 'test',
];
$rules = $this->my_validations->get_rules($params);
echo 'rules <br>';
print_r($rules);
$this->form_validation->set_rules($rules);
echo 'validate';
$validate = $this->form_validation->run();
echo '<pre>'.($validate ? 'OK' : 'failed').'</pre>'; // always 'failed'
}
$this->load->view('form', $this->data);
}
}

这是表格:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form method="post" action="">
<input type="hidden" id="csrf" name="<?php echo $csrf_name; ?>" value="<?php echo $csrf; ?>">
<div>
<label>Phone</label>
<input type="text" name="phone" value="<?php echo $this->input->post('phone'); ?>">
</div>

<input type="submit" value="Kirim">
</form>
</body>
</html>

表单验证回调只会在当前控制器内的方法上触发。或者扩展CI_Form_validation类:

class MY_Form_validation extends CI_Form_validation {

function __constuct() {
parent::__constuct();
}
public function get_rules($params=[])
{
$rules = [];
$module = $params['module'];
if ($module == 'test')
{
$rules[] = [
'field' => 'phone',
'label' => 'test phone 1',
'rules' => '_test_rule1'
];
}
return $rules;
}
// end public function get_rules
public function _test_rule1()
{
return TRUE;
}
}

不要忘记删除";callback_;规则中的前缀。

之后你的控制器将看起来像:

<?php
/*
* filename: test1.php
* filedir: application/modules/test/controllers/
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends MY_Controller 
{
public function test1()
{
$this->load->library('form_validation');
//$this->load->library('My_validations');
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
$params = [
'module' => 'test',
];
//$rules = $this->my_validations->get_rules($params);
$rules = $this->form_validation->get_rules($params);
echo 'rules <br>';
print_r($rules);
$this->form_validation->set_rules($rules);
echo 'validate';
$validate = $this->form_validation->run();
echo '<pre>'.($validate ? 'OK' : 'failed').'</pre>'; // always 'failed'
}
$this->load->view('form', $this->data);
}
}

相关内容

  • 没有找到相关文章

最新更新