如何为CodeIgniter动态设置表单验证规则?

编程入门 行业动态 更新时间:2024-10-25 08:27:31
本文介绍了如何为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();

更多信息: codeigniter/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动态设置表单验证规则?

本文发布于:2023-11-23 03:30:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619919.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   何为   规则   动态   CodeIgniter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!