duansha6410
2014-04-18 23:03
浏览 118
已采纳

$ this-> form_validation-> run()返回FALSE而没有验证规则

When using CodeIgniter's Form_validation class, the set_rules() method allows a user to add an infinite number of checks to their form fields. These rules are usually all defined in the same place, right before the run() method is called.

VALID EXAMPLE:

// Require the user to be a werewolf.
$this->form_validation->set_rules('werewolf', 'Are you a Werewolf?', 'required');

// If the form validates.
if ($this->form_validation->run())
{
    // [...]

(no, I do not actually run a werewolf website)

The code makes a lot of sense, and is very easy to understand. However, at times, it may be undesirable. For example, perhaps you don't care if the user is a werewolf unless it is midnight. Your code would probably look like this:

INVALID EXAMPLE

// If it is midnight.
if (date('G') == '0')
{
    // Require the user to be a werewolf.
    $this->form_validation->set_rules('werewolf', 'Are you a Werewolf?', 'required');
}

// If the form validates.
if ($this->form_validation->run())
{
    // [...]

THIS WON'T WORK

At least, it won't work until midnight.

By default, $this->form_validation->run() returns FALSE if there aren't any rules declared. In the second example, unless it's midnight, there will not be any rules set, so the form will never validate. Logically, one would assume that if there are no rules, the form should validate no matter what. Instead, it fails no matter what.

/system/libraries/Form_validation :: run()

(lines 293-297)

// No validation rules?  We're done...
if (count($this->_config_rules) == 0)
{
    return FALSE;
}

My question is:

Why is this the default return value? Is it okay if I change it?

图片转代码服务由CSDN问答提供 功能建议

使用CodeIgniter的 Form_validation 类时, set_rules() 方法允许用户向其表单字段添加无限数量的检查。 这些规则通常都在同一位置定义,就在调用 run()方法之前。

有效范例:< / p>

  //要求用户成为狼人。
 $ this-&gt; form_validation-&gt; set_rules('狼人','你是狼人吗?','必需 '); 
 
 //如果表单验证。
if($ this-&gt; form_validation-&gt; run())
 {
 // [...] 
   
 
 

(不,我实际上并没有运行狼人网站)

代码很有意义,并且很容易理解。 但是,有时候,这可能是不受欢迎的。 例如,您可能不关心用户是否是狼人除非是午夜。 您的代码可能如下所示:

无效示例

  //如果是午夜。  nif(日期('G')=='0')
 {
 //要求用户成为狼人。
 $ this-&gt; form_validation-&gt; set_rules('狼人','你是 Werewolf?','required'); 
} 
 
 //如果表单验证。
if($ this-&gt; form_validation-&gt; run())
 {
 // [...  ] 
   
 
 

将无效

至少 ,它将无法工作到午夜。

默认情况下, $ this-&gt; form_validation-&gt; run()返回 FALSE 如果没有声明任何规则。 在第二个示例中,除非是午夜,否则不会设置任何规则,因此表单永远不会验证。 从逻辑上讲,人们会认为如果没有规则,表格无论如何都应该验证。 相反,失败无论如何。

/ system / libraries / Form_validation :: run()

(第293-297行)

  //没有验证规则? 我们完成了... 
if(count($ this-&gt; _config_rules)== 0)
 {
返回FALSE; 
} 
   
 
 

我的问题是:

为什么这是默认的返回值? 如果我改变它可以吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanpo1821 2014-04-19 01:27
    已采纳

    @Nathanael - you are right, I can't recall without looking if this has been addressed in V3 or no.

    Two ways of dealing with this:

    1) (this won't work the way your code is currently set up, but w/ a small teak it will) - Set "trim" on every field, all the time. This will trigger all the validation code. It is an old workaround for repopulating forms even when you don't want to make anything required.

    2) Better - make a custom rule for that field that is ALWAYS checked, and require it conditionally inside the rule.

    Remember that validation library rules are actually just functions on the lib, so inside your custom rule you can do:

    $this->validation->required($str);
    

    type code to check your input.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongwei4103 2015-11-27 01:16

    Just set empty rule, the validation lib will auto by pass it and continue.

    $this->form_validation->run() will return true instead.

    $this->form_validation->set_rules(fieldName,'','');
    

    reference https://github.com/bcit-ci/CodeIgniter/issues/1558

    Take your code as example, your code will be like this

    // If it is midnight.
    if (date('G') == '0') {
        // Require the user to be a werewolf.
        $this->form_validation->set_rules('werewolf', 'Are you a Werewolf?', 'required');
    } else {
        $this->form_validation->set_rules('werewolf', '', '');
    }
    
    评论
    解决 无用
    打赏 举报