0

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

我正在尝试这个

在控制器中

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

我有功能

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

我正在尝试在此处添加自定义回调 url test。但不工作

我正在尝试这个例子

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

但我确实喜欢这个验证有效

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

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

4

1 回答 1

3

您的自定义回调函数需要返回 TRUE 或 FALSE 才能正常工作。另外,在die()这里发表声明并不能真正帮助您...

这是一个简单的代码示例,但我希望你能明白:

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

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

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

$this->form_validation->set_message('test', 'The value you provided is not in the right format');
于 2011-10-18T08:10:33.457 回答