Suppose this is my controller. (copied from CI documentation)
假设这是我的控制器。 (从CI文档复制)
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
But username_check($str) function is public. According to CI documentation if i want to make a private method I need to add "_" like this.
但是username_check($ str)函数是公共的。根据CI文档,如果我想制作私有方法,我需要像这样添加“_”。
private function _utility()
{
// some code
}
But how can I set username_check() as private and callback from form validation set_rules?
但是如何将username_check()设置为私有和来自表单验证set_rules的回调?
Should i use DOUBLE underscore "__" i.e callback__username_check?
我应该使用DOUBLE下划线“__”,即callback__username_check吗?
1 个解决方案
#1
1
You can just declare your private function as you have already done:
你可以像你已经完成的那样声明你的私有函数:
function _username_check()
{
// some code
}
And in the validation rule, use:
在验证规则中,使用:
callback__username_check
As I have seen, this must work just fine!
正如我所见,这必须正常工作!
Remember:
记得:
The _
prefix would take care of your function privacy, so you do not really have to use the keyword private
to declare the function to let the form_validation
class access to that function!
_前缀会处理你的函数隐私,所以你不必使用关键字private来声明函数让form_validation类访问该函数!
#1
1
You can just declare your private function as you have already done:
你可以像你已经完成的那样声明你的私有函数:
function _username_check()
{
// some code
}
And in the validation rule, use:
在验证规则中,使用:
callback__username_check
As I have seen, this must work just fine!
正如我所见,这必须正常工作!
Remember:
记得:
The _
prefix would take care of your function privacy, so you do not really have to use the keyword private
to declare the function to let the form_validation
class access to that function!
_前缀会处理你的函数隐私,所以你不必使用关键字private来声明函数让form_validation类访问该函数!