【问题标题】:CodeIgniter Anonymous Form ValidationCodeIgniter 匿名表单验证
【发布时间】:2018-06-15 07:24:24
【问题描述】:

我有如下代码:

$this->form_validation->set_rules("codetype", "Code Type", array(
        "codecheck" => function ($str) {
            // just return it false.
            return false;
        }
    ), array("codecheck"=>"return this message"));

我希望它返回代码检查错误消息。但 codeigniter 表单验证类返回此消息:

“无法访问与您的字段名称对应的错误消息 代码类型”。

如何编写带有错误消息的完全匿名 CodeIgniter 函数?

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

    希望对您有所帮助:

    您可以根据需要删除required,并设置您的if 条件

    $this->form_validation->set_rules('codetype', 'Code Type',
           array(
              'required',
              array(
                    'codecheck_callable',
                    function($str)
                    {
                      // Check validity of $str and return TRUE or FALSE
                      if ($str == 'test') 
                      {
                         $this->form_validation->set_message('codecheck_callable', 'can not be test');
                         return false;
                       }
                       else
                       {
                          return TRUE;
                       }
                   }
              )
         )
    );
    

    更多:https://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

    【讨论】:

    • 如果验证是匿名的,可能 codeigniter 表单验证自定义错误消息不能直接在 set_rules 函数上工作。我的意思是 set_rules 的第三个参数。但这有效。谢谢。
    【解决方案2】:

    根据 https://www.codeigniter.com/userguide3/libraries/form_validation.html 的 CodeIgniter 文档

    你可以这样做

    $this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
    

    【讨论】:

    • 对不起,但这也不适用于匿名函数。我还想要一个完全匿名的。它也可以在 codeigniter 文档中使用,但它不起作用
    • 然后尝试将最后一个参数从 array("codecheck"=>"return this message") 更改为 array("codetype"=>"return this message")
    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 2016-09-05
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多