Yii2:使用 OR 对多个属性进行自定义验证不起作用 [英] Yii2: custom validation for multiple attributes using OR not working

查看:25
本文介绍了Yii2:使用 OR 对多个属性进行自定义验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个规则来验证是否设置了 attribute_a 或 attribute_b;

I am trying to write a rule that validates if attribute_a or attribute_b is set;

必须设置以下属性之一:licitatii_publicelicitatiile_atribuite

one of the following attributes must be set : licitatii_publice or licitatiile_atribuite

以下代码无效;

<?php

namespace common\models;

use yii\base\Model;

class AbonamentValidare extends Model {

    public $licitatii_publice;
    public $licitatiile_atribuite;
    public $zone;
    public $judete;
    public $tari;
    public static $targetAttribute = [];

    public function rules() {
        return [
            [['zone'], 'required'],
            [['licitatii_publice', 'licitatiile_atribuite', 'tari', 'judete'], 'safe'],
            ['licitatii_publice', 'validate_tip_licitatie', 'targetAttribute' => ['licitatii_publice', 'licitatiile_atribuite']],
        ];
    }

    function validate_tip_licitatie($attribute, $param) {
        print_r($attribute);
        $this->addError($attribute, 'eroarea');
    }

    public function attributeLabels() {
        return array(
            'licitatii_publice' => 'lp',
            'licitatiile_atribite' => 'la',
            'tari' => 'tari',
            'judete' => 'judete',
            'zone' => 'zone',
        );
    }

    public function save() {
        return false;
    }

}

?>

推荐答案

我在这种情况下所做的就是创建这样的验证器:

Well what I have done in a case like this is to create the validator like this:

................
    return [
                [['zone'], 'required'],
                [['licitatii_publice', 'licitatiile_atribuite', 'tari', 'judete'], 'safe'],
                [['licitatii_publice, licitatiile_atribuite'], 'validate_tip_licitatie'],
            ];
............
    function validate_tip_licitatie($attribute, $param) {
        if(!$this->licitatii_publice && $this->licitatiile_atribuite)
        $this->addError($attribute, 'eroarea');
    }

通过这种方式,您可以同时显示两个字段并显示错误.

In this way you show both fields with an error.

但是我在 Yii1 中已经这样做了,但是从我读到的 Yii2 应该是一样的.逻辑是一样的.

However I have done this in Yii1, but from what I read Yii2 should be the same. The logic is the same.

如果您只想显示 1 个属性的错误,您可以随时使用

If you want to show error only for 1 attribute you can always just use

return [
            [['zone'], 'required'],
            [['licitatii_publice', 'licitatiile_atribuite', 'tari', 'judete'], 'safe'],
            [['licitatii_publice'], 'validate_tip_licitatie'],
        ];

你想要做的更花哨:),我明白了.如果你真的想使用 targetAttribute 你可能必须这样做https://github.com/yiisoft/yii2/blob/master/framework/validators/ExistValidator.php

What you are trying to do is more fancy :), I get that. If you really want to use targetAttribute you might have to do it like this https://github.com/yiisoft/yii2/blob/master/framework/validators/ExistValidator.php

只需构建您自己的验证器类.

Just build your own validator class.

嗯.在阅读了存在验证器之后,我相信这正是您所需要的.它有关于如何使用它的示例.

Well. After reading about the exist validator i believe that is exactly what you need. It has examples on how to use it.

这篇关于Yii2:使用 OR 对多个属性进行自定义验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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