Yii表单模型验证 - 需要一个 [英] Yii form model validation- either one is required
问题描述
我在表单上有两个字段(忘记密码表单)用户名和电子邮件ID。用户应该输入其中之一。我的意思是检索密码用户可以输入用户名或电子邮件ID。有人可以指出我的验证规则吗?
是否有任何内置规则可以使用?
(对不起,如果它已被讨论或如果我错过了)
感谢您的帮助
问候
基兰
今天我试图解决同样的问题。
公共函数规则()
{
返回数组(
// array('username,email','required'),//从所需字段中删除这些字段
array('email','email'),
array( '用户名,电子邮件','my_equired'),//在用户名和电子邮件字段
)的任何验证下执行此操作。
public function my_required($ attribute_name,$ params)
{
if(empty($ this-> username)
&& amp ;空($ this->电子邮件)
){
$ this-> addError($ attribute_name,Yii :: t('user','必须填写至少1个字段正确'));
返回false;
}
返回true;
}
一般想法是将'required'验证移动到自定义my_required()可以检查是否有任何字段已满。
我看到这篇文章是从2011年开始的,但是我找不到任何其他解决方案。我希望它能为你或其他未来工作。
享受。
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)
Thanks for your help
Regards
Kiran
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;
}
General idea is to move 'required' validation to custom my_required() method which can check if any of field is filled up.
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.
Enjoy.
这篇关于Yii表单模型验证 - 需要一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!