【问题标题】:codeigniter custom form validation call back url is not workingcodeigniter 自定义表单验证回调 url 不起作用
【发布时间】:2011-12-09 21:21:35
【问题描述】:

您好,我正在使用 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 功能不起作用。请帮我 。谢谢........

【问题讨论】:

    标签: php codeigniter validation codeigniter-form-helper


    【解决方案1】:

    您的自定义回调函数需要返回 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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2014-09-24
      • 2012-10-04
      • 1970-01-01
      相关资源
      最近更新 更多