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

时间:2018-09-11 14:43:08

标签: php model yii2

如何为多个属性的组合指定模型中的唯一规则? 我想要一个规则,不允许插入具有相同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'],
];

1 个答案:

答案 0 :(得分:0)

您可以在模型中使用CompareValidator。

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

有关更多信息,请参见docs