使用新版本的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动态设置表单验证规则?
发布评论