Yii Framework Forum: How Can I Validate Either One Of The Textfields To Be Filled In Yii - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How Can I Validate Either One Of The Textfields To Be Filled In Yii Rate Topic: -----

#1 User is offline   Rudra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 12-July 13

Posted 29 January 2014 - 03:36 AM

I'm new to Yii framework. Now in my form I have two fields FirstName and LastName.
I want to validate such that either of the two is filled. i.e not both should be empty.
Suppose the user leaves both the fields empty it should not allow submit. The user should atleast enter any of these fields.
**Rules**

public function rules()
{
return array(

array('Firstname,Lastname, email, subject, body', 'required'),
array('email', 'email'),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
);
}

How can I do this?
0

#2 User is offline   Fabrizio Caldarelli 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 284
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 29 January 2014 - 03:40 AM

I think you have two solution:

1) Create a custom validate;
2) Subclass beforeValidate() method of your model and insert rules to check;

In both solution you need to remove that two fields from 'require' validator.
0

#3 User is offline   Rudra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 12-July 13

Posted 29 January 2014 - 03:46 AM

View PostFabrizio Caldarelli, on 29 January 2014 - 03:40 AM, said:

I think you have two solution:

1) Create a custom validate;
2) Subclass beforeValidate() method of your model and insert rules to check;

In both solution you need to remove that two fields from 'require' validator.

Can you please explain in detail. I'm new to Yii framework. I did not get you
0

#4 User is offline   Fabrizio Caldarelli 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 284
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 29 January 2014 - 03:51 AM

First solution.
Read this:
http://www.yiiframew...alidation-rule/
http://www.yiiframework.com/wiki/56/


Second solution.
In your model insert this method:

public function beforeValidate()
{
     if (parent::beforeValidate())
     {
     	if (($this->name==null)&&($this->surname==null))      
	{
		$this->addError('name', 'name or surname should be filled');
		return false;
	}

        return true;
     }
     return false;
}


Remember to remove name and surname fields from 'require' validator in rules.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users