简体   繁体   English

Yii2模型独特规则,用于多个属性的组合

[英]Yii2 Model Unique Rules for combination of more than one attribute

How can I specify a unique rule in my models for a combination of more that one attributes? 如何在模型中为多个属性的组合指定唯一规则? I want a rule that can't allow insert of a record with a combination of the same FIELD100 , FIELD3 我想要一个规则,不允许插入具有相同FIELD100FIELD3的组合的记录

return [
    [['FEETYPE_F_V_R_','FIELD32','IS_CUSTOMER_EXPENSE','IS_BANK_EXPENSE','IS_BANK_EXPENSE','FIELD100'], 'required'],
    [['ID', 'ACTIVE', 'APPROVED', 'REWORKED', 'IS_CUSTOMER_EXPENSE', 'IS_BANK_EXPENSE', 'IS_BANK_EXPENSE'], 'integer'],
    [['AMOUNT'], 'number'],
    [['REWORKEDON'], 'safe'],
    [['FIELD3', 'FIELD32'], 'string', 'max' => 10],
    [['FEETYPE_F_V_R_', 'FIELD24'], 'string', 'max' => 2],
    [['FIELD100', 'CREATEDBY', 'APPROVEDBY', 'REWORKEDBY'], 'string', 'max' => 50],
    [['CREATEDDATE', 'APPROVEDDATE'], 'string', 'max' => 7],
    [['MEMOCODE'], 'string', 'max' => 20],
    [['ID'], 'unique'],
];

You can use CompareValidator in your Model. 您可以在模型中使用CompareValidator。

public function rules()
{
    return [ 
        [
            'FIELD100',
            'compare',
            'compareAttribute' => 'FIELD3',
            'operator' => '!=',
            'message' => 'Both values can not be the same'
        ]
    ];
}

For more info see the docs 有关更多信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM