假设这是我的控制器。(从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;
}
}
}
?>
但是username_check($ str)函数是公共的。根据CI文档,如果我想使用私有方法,则需要添加“ _”。
private function _utility()
{
// some code
}
但是,如何将username_check()设置为private并从表单验证set_rules回调呢?
我应该使用双下划线“ __”,即callback__username_check吗?
您可以像已经完成的那样声明私有函数:
function _username_check()
{
// some code
}
在验证规则中,使用:
callback__username_check
如我所见,这必须很好!
记得:
该_
前缀将采取你的函数隐私照顾,所以你真的没有使用关键字private
来声明功能,让form_validation
类访问该功能!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句