codeigniter自定义表单验证回调网址无法正常工作

时间:2011-10-18 08:05:31

标签: php codeigniter validation codeigniter-form-helper

您好我正在使用codeigniter表单验证,我想为我的price字段添加自定义表单验证

我正在尝试这个

控制器中的

$this->ci->form_validation->set_rules ( 'price', 'Price', 'callback_test' ); 

我有功能

private function test()
{
    echo "hello"; die ;
}

我正在尝试在此处添加自定义回拨网址test。但是没有工作

我正在尝试这个例子

http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks

但我确实喜欢验证工作

$this->ci->form_validation->set_rules ( 'price', 'Price', 'required' );

为什么我的回调网址功能不起作用。请帮我 。感谢..............

1 个答案:

答案 0 :(得分:3)

您的自定义回调函数需要返回TRUE或FALSE才能正常工作。另外,在这里添加die()语句并没有真正帮助你......

这是一个简单的代码示例,但我希望你能得到这样的结论:

$this->form_validation->set_rules ( 'price', 'Price', 'required|callback_test' );

function test($string)
{
   return ($string == 'something') ? TRUE : FALSE;
}

$string是来自input->帖子的值,会自动传递给您的回调函数。 您还需要为此回调指定一条错误消息,否则您将收到“没有为自定义字段提供错误消息”的错误,或者类似的错误消息。

$this->form_validation->set_message('test', 'The value you provided is not in the right format');