如何为CodeIgniter动态设置表单验证规则? [英] How to Set Form Validation Rules for CodeIgniter Dynamically?

查看:117
本文介绍了如何为CodeIgniter动态设置表单验证规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新版本的CodeIgniter;您只能在静态的 form_validation.php 文件中设置规则。我需要分析发布的信息(即,只有当他们选择一个复选框)。只有这样,我才需要验证某些字段。

With the new version of CodeIgniter; you can only set rules in a static form_validation.php file. I need to analyze the posted info (i.e. only if they select a checkbox). Only then do I want certain fields to be validated. What's the best way to do this, or must I use the old form validation class that is deprecated now?

推荐答案

您不仅可以使用这种方法来执行此操作,在config / form_validation.php文件中设置规则。您也可以使用以下设置:

You cannot only set rules in the config/form_validation.php file. You can also set them with:

   $this->form_validation->set_rules();

更多信息: http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules

但是,CI的首选顺序是首先检查是否有set_rules()设置的规则,如果没有,请查看是否有在配置文件中定义的规则。

However, the order of preference that CI has, is to first check if there are rules set with set_rules(), if not, see if there are rules defined in the config file.

因此,如果您在配置文件中添加了规则,但是在操作中调用了set_rules(),则将无法访问配置规则。

So, if you have added rules in the config file, but you make a call to set_rules() in the action, the config rules will never be reached.

知道,对于条件验证,我将在模型中有一个特定的方法,根据输入(对于特定操作)初始化form_validation对象。我需要这样做的典型情况是验证运费和帐单地址(是相同还是不同)。

Knowing that, for conditional validations, I would have a specific method in a model that initializes the form_validation object depending on the input (for that particular action). The typical situation where I've had the need to do this, is on validating shipping and billing addresses (are they the same or different).

希望有帮助。 :)

这篇关于如何为CodeIgniter动态设置表单验证规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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