Yii 表单模型验证 - 需要任何一个 [英] Yii form model validation- either one is required
问题描述
我在表单(忘记密码表单)用户名和电子邮件 ID 上有两个字段.用户应输入其中之一.我的意思是检索密码用户可以输入用户名或电子邮件 id .有人能指出我对此的验证规则吗?
I have two fields on the form ( forgotpassword form ) username and email Id . User should enter one of them . I mean to retrieve the password user can enter user name or the email id . Could some one point me the validation rule for this ?
有什么我可以使用的内置规则吗?
Is there any inbuilt rule I can use ?
(对不起,如果已经讨论过或者我错过了)
( Sorry if it is already discussed or if I missed)
感谢您的帮助
问候
基兰
推荐答案
我今天试图解决同样的问题.我得到的是下面的代码.
I was trying to solve same problem today. What I've got is the code below.
public function rules()
{
return array(
// array('username, email', 'required'), // Remove these fields from required!!
array('email', 'email'),
array('username, email', 'my_equired'), // do it below any validation of username and email field
);
}
public function my_required($attribute_name, $params)
{
if (empty($this->username)
&& empty($this->email)
) {
$this->addError($attribute_name, Yii::t('user', 'At least 1 of the field must be filled up properly'));
return false;
}
return true;
}
总体思路是将必需"验证移至自定义 my_required() 方法,该方法可以检查是否有任何字段已填写.
General idea is to move 'required' validation to custom my_required() method which can check if any of field is filled up.
我看到这篇文章是 2011 年的,但我找不到任何其他解决方案.我希望它将来对您或其他人有用.
I see this post is from 2011 however I couldn't find any other solution for it. I Hope it will work for you or other in the future.
享受.
这篇关于Yii 表单模型验证 - 需要任何一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!