Yii Framework Forum: conditional form rule - Yii Framework Forum

Jump to content

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

conditional form rule Rate Topic: -----

#1 User is offline   vassy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 14-October 09

Posted 08 February 2010 - 07:48 AM

Hello.

How can I enable/disable form rule using some condition?
Example:
public function rules()
	{
		return array(
			array('email', 'email', 'enable' => app()->param['checkEmail'])
		);
	}

So rule is validated if app->param['cehckEmail'] is true.

Thank you.
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 08 February 2010 - 08:22 AM

View Postvassy, on 08 February 2010 - 07:48 AM, said:

Hello.

How can I enable/disable form rule using some condition?
Example:
public function rules()
	{
		return array(
			array('email', 'email', 'enable' => app()->param['checkEmail'])
		);
	}

So rule is validated if app->param['cehckEmail'] is true.

Thank you.


Try this
public function rules()
{
  return array(
    array(app()->param['checkEmail']?'email':'', 'email')
  );
}


Edit:
I accidentally modified the attribute instead of the rule specifier. This is what I first intended to do:
public function noRule($attribute,$params) { }

public function rules()
{
  return array(
    array('email', app()->param['checkEmail']?'email':'noRule')
  );
}


/Tommy

This post has been edited by tri: 08 February 2010 - 09:11 AM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   ps_sach 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 07-December 09

Posted 08 February 2010 - 08:37 AM

I want to validate the field if and only if the field is not empty, how should I do it?

For example, I don't want to put require restriction on the email field, but if it is entered it should be checked for valid email address.

Thanks!
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 08 February 2010 - 09:05 AM

View Postps_sach, on 08 February 2010 - 08:37 AM, said:

I want to validate the field if and only if the field is not empty, how should I do it?

For example, I don't want to put require restriction on the email field, but if it is entered it should be checked for valid email address.

Thanks!


If I understand your question correctly, use the 'email' validator but omit the 'required' validator.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   aztech 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 206
  • Joined: 12-December 08
  • Location:Poland

Posted 08 February 2010 - 09:10 AM

There is magic attribute called allowEmpty :)
I'm not complete idiot... some parts are missing!
0

#6 User is offline   ps_sach 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 07-December 09

Posted 08 February 2010 - 09:20 AM

Thank you all of you for your replies!
And if I want to validate the fields which are dependent on values of other fields, how should I do it?

For example, I have a check box for advanced inputs. If user checks it, I need to apply the validation.

Thanks again!
0

#7 User is offline   vassy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 14-October 09

Posted 08 February 2010 - 09:59 AM

tri, thanks!

Your solution is what I was looking for.
0

#8 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 751
  • Joined: 02-February 09

Posted 09 February 2010 - 04:24 PM

View Postps_sach, on 08 February 2010 - 09:20 AM, said:

Thank you all of you for your replies!
And if I want to validate the fields which are dependent on values of other fields, how should I do it?

For example, I have a check box for advanced inputs. If user checks it, I need to apply the validation.

Thanks again!



you can always declare custom function in your rules such as:

public function rules()
	{
     return array(
            array('input_field','check'),
  }
}

protected function check()
{
 validate as you wish here and if your application hits into an error just do:
  $this->addError('input_field','error message');
}


hope this helps
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