基本上我想在Form_validation
的扩展类中使用回调验证方法 。
确定案例
我使用公共回调方法进行MY_Form_validation
课程。
在我的控制器中加载为:
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
在此控制器中用作:
$this->form_validation->validate();
我在自定义表单类中使用它:
$this->CI->form_validation->set_rules('field', 'field', 'check_callback');
请注意,它不会被称为callback_<name of method>
,我不知道为什么仅在调用时没有callback_
前缀在自己的班级里面。
NOK CASE
我使用公共回调方法进行MY_Custom_form_validation
课程。
在我的控制器中加载为:
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('my_custom_form_validation');
}
$this->my_custom_form_validation->validate();
在set_rules
中使用与OK CASE相同的设置,但这次我收到以下错误:
Unable to access an error message corresponding to your field name field.(check_callback)
为什么会这样?他们只是文件名和加载方式不同!我在这里错过了什么吗?
或者只应该有一个扩展MY_Form_validation
,它将包含整个项目的整个验证? CI是否真的会阻止 MY_Form_validation
之外的自定义类?
MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation
{
protected $CI;
public function __construct($rules = array())
{
parent::__construct($rules);
$this->CI =& get_instance();
}
public function validate()
{
$this->CI->form_validation->set_rules('value', 'value',
'trim|required|integer|check_value['.$this->CI->input->post('unit').']');
$this->CI->form_validation->set_rules('unit', 'unit', 'trim|required');
}
public function check_value($value, $unit)
{
if($value >= 100)
{
return TRUE;
}
else
{
$this->CI->form_validation->set_message('check_value',
'The %s must be greater than or equal 100.');
return FALSE;
}
}
MY_Custom_form_validation.php
<?php
class MY_Custom_form_validation extends CI_Form_validation
{
protected $CI;
public function __construct($rules = array())
{
parent::__construct($rules);
$this->CI =& get_instance();
}
public function validate()
{
$this->CI->form_validation->set_rules('value', 'value',
'trim|required|integer|check_value['.$this->CI->input->post('unit').']');
$this->CI->form_validation->set_rules('unit', 'unit', 'trim|required');
}
public function check_value($value, $unit)
{
if($value >= 100)
{
return TRUE;
}
else
{
$this->CI->form_validation->set_message('check_value',
'The %s must be greater than or equal 100.');
return FALSE;
}
}
Form.php
,请注意 OK CASE 和不行
<?php
class Form extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
// OK CASE
$this->load->library('form_validation');
// NOK CASE
$this->load->library('my_custom_form_validation');
$this->load->model('prf_model');
}
public function submit()
{
$data['username'] = 'my_username';
$data['title'] = 'Submit a PRF';
$data['last_order_submission'] =
($this->prf_model->get_last_prf_order_submission()+1);
// OK CASE
$this->form_validation->validate();
// NOK CASE
$this->my_custom_form_validation->validate();
if(!$this->form_validation->run())
{
$this->load->view('templates/header', $data);
$this->load->view('pages/form', $data);
$this->load->view('templates/footer', $data);
}
else
{
$this->_save_to_database($_POST);
redirect('form/success');
}
}
答案 0 :(得分:1)
之前我遇到过同样的问题,在CI中你不能使用任何自定义验证名称,它应该是MY_Form_validation。检查一下,Code Igniter Custom Validation