$ this-> form_validation-> run()在没有验证规则的情况下返回FALSE [英] $this->form_validation->run() returns FALSE without validation rules

查看:406
本文介绍了$ this-> form_validation-> run()在没有验证规则的情况下返回FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用CodeIgniter的 Form_validation 类时, set_rules()方法允许用户添加无限数量的检查到他们的形式领域。这些规则通常都定义在相同的地方,就在调用 run()方法之前。



strong> VALID示例:

  //要求用户成为狼人。 
$ this-> form_validation-> set_rules('werewolf','你是狼人吗?','required');

//如果表单验证。
if($ this-> form_validation-> run())
{
// [...]

(不,我实际上没有运行一个werewolf网站)



代码很有意思,是很容易理解。然而,有时,这可能是不期望的。例如,也许你不在乎用户是否是狼人,除非是午夜。您的代码可能如下所示:



INVALID EXAMPLE

  //如果是午夜。 
if(date('G')=='0')
{
//要求用户成为狼人。
$ this-> form_validation-> set_rules('werewolf','你是狼人吗?','required');
}

//如果表单验证。
if($ this-> form_validation-> run())
{
// [...]






这将无法工作



<



默认情况下, $ this-> form_validation-> run()如果没有声明任何规则,则返回 FALSE 。在第二个示例中,除非是午夜,否则不会有任何规则集,因此表单将永远不会生效。逻辑上,人们会认为,如果没有规则,表格应该验证,无论什么。



/ system / libraries / Form_validation :: run() p>

(第293-297行)

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

我的问题是:



为什么是默认返回值?

解决方案

@ Nathanael - 你说得对,我不记得了



两种处理方式:



1) (这将不工作的方式你的代码当前设置,但w /一个小柚木它会) - 设置修剪每个字段,所有的时间。这将触发所有验证代码。



2)更好 - 为该字段创建一个始终选中的自定义规则



请记住,验证库规则实际上只是lib上的函数,因此在你的自定义规则中你可以这样做:

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

键入代码以检查您的输入。


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?

解决方案

@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.

这篇关于$ this-&gt; form_validation-&gt; run()在没有验证规则的情况下返回FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆