YII2 验证检查在两个字段之间是唯一的,而不使用活动记录 [英] YII2 validation check unique between two fields without using active record
本文介绍了YII2 验证检查在两个字段之间是唯一的,而不使用活动记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了两个字段old_password"和new_password".如果两个字段中的值相同,我想要错误消息.我没有使用 Activerecords.我在模型中尝试过:
I am using two fields "old_password" and "new_password". I want the error message if value in both fields are same. I am not using Activerecords. I tried in model :
['a1', 'unique', 'targetAttribute' => 'a2']
但以上代码仅适用于活动记录.如何在不使用活动记录的情况下获取错误消息?
but above code will work only for active record. How can i get error message without using active record ?
推荐答案
您需要使用 比较 验证器而不是唯一的.
You need to use compare validator instead of unique.
['new_password', 'compare', 'compareAttribute' => 'old_password', 'operator' => '!='],
因为唯一验证器验证属性值在整个表中是唯一的
Because unique validator validates that the attribute value is unique across the table
这篇关于YII2 验证检查在两个字段之间是唯一的,而不使用活动记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文