对多个字段应用相同的验证规则 [英] Apply same validation rule for multiple fields

查看:87
本文介绍了对多个字段应用相同的验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在2.0中为50个字段应用相同的验证规则

How do i apply same validation rule for 50 fields in 2.0

我不想为不同字段重复规则

i am not interested in repeating rule for different field

public $validate = array(
    'company' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Cannot be Empty',

        ),
    ),
    // rule for other 50 fields....
);


推荐答案

可能的解决方案:

$validate_items = array('company', 'other', 'one_more');

$validate_rule = array(
    'notempty' => array(
        'rule' => array('notempty'),
        'message' => 'Cannot be Empty')
    );

$validate = array();

foreach ($validate_items as $validate_item) {
    $validate[$validate_item] = $validate_rule;
}

echo "<pre>".print_r($validate, true)."</pre>";

不明白为什么要确定相同的验证50次。

Don't understand why you want to determinate same validation 50 times. You can declare just one rule and use it for all your fields.

可能是我误解了你的问题?

May be I misunderstood your question?

这篇关于对多个字段应用相同的验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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