CodeIgniter调用自定义form_validation类中的回调 [英] CodeIgniter call a callback within a custom form_validation class
问题描述
基本上我想使用里面一个回调验证方法 Form_validation
。
确定CASE
我使用公共回调方法创建 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_< code>这是我不知道为什么只有工作没有
callback _
前缀
我创建
在我的控制器中加载:
$ my_custom_form_validation pre> 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
但这次我收到以下错误:
无法访问与您的字段名称字段(check_callback)
为什么会发生这种情况?他们只有不同的文件名和加载方式!我在这里错过了什么吗?
或者只有一个扩展 MY_Form_validation
,将包含整个项目的整个验证?除了 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',
'%s必须大于或等于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',
'%s必须大于或等于100。
return FALSE;
}
}
Form.php
,请注意确定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'] ='提交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');
}
}
I面对同样的问题之前,在CI你不能只使用任何自定义验证名称,它应该是MY_Form_validation。选中此项,代码Igniter自定义验证
Basically I want to use a callback validation method inside an extended class of Form_validation
.
OK CASE
I make MY_Form_validation
class with the public callback method.
Loaded in my controller as:
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
used in this controller as:
$this->form_validation->validate();
I use it like this inside the custom form class:
$this->CI->form_validation->set_rules('field', 'field', 'check_callback');
Note that it is not called as callback_<name of method>
which is I do not know either why it only works without the callback_
prefix when called inside its own class.
NOK CASE
I make MY_Custom_form_validation
class with the public callback method.
Loaded in my controller as:
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();
Same settings as OK CASE when used in set_rules
but this time I get the following error:
Unable to access an error message corresponding to your field name field.(check_callback)
Why is this happening? They only differed in file name and way of loading! Did I miss something here?
Or there should only be ONE extended MY_Form_validation
that will contain the entire validation for the entire project? Does CI really prevents this custom class aside from 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
, please note of the OK CASE and NOT OK
<?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');
}
}
I faced the same issue before, in CI you cant just use any custom validation name it should be MY_Form_validation. Check this, Code Igniter Custom Validation
这篇关于CodeIgniter调用自定义form_validation类中的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!