Yii Framework Forum: [Extension] Yii Conditional Validator - Yii Framework Forum

Jump to content

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

[Extension] Yii Conditional Validator Use core validation based on conditions Rate Topic: -----

#1 User is offline   sidtj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 22-October 10

Posted 18 January 2012 - 07:39 PM


General Information ¶
Yii Conditional Validator validates any number of attributes if certain attribute validation is true.

You can use any Yii Core Validator like usually you would do or any other Class Based/Inline validator. You can even use CASCADE validation using Yii Conditional Validator itself;




Yii Conditional Validator Page




--

Sidney Lins
sidtj


Yii Smart Menu
Let YSM automagically hide/show your cmenu items according to result of checkAccess() function.

Yii Conditional Validator
Make validation using core validators based in conditions.


Posted ImageLista Yii Framework Brasil
A maior e mais ativa lista de discussão por e-mail em português.

Participe!
0

#2 User is offline   sidtj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 22-October 10

Posted 19 January 2012 - 07:03 AM

Online documentation updated.

--

Sidney Lins
sidtj


Yii Smart Menu
Let YSM automagically hide/show your cmenu items according to result of checkAccess() function.

Yii Conditional Validator
Make validation using core validators based in conditions.


Posted ImageLista Yii Framework Brasil
A maior e mais ativa lista de discussão por e-mail em português.

Participe!
0

#3 User is offline   sidtj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 22-October 10

Posted 19 January 2012 - 12:50 PM


Coming soon
New option to allow use dot notation on a validator attribute to get related data and use in your validation. Example:

When creating a
customer
, assuming that a business is selected, one might need check the
customer.business.city
(assuming business is the relation name in Customer) to validate if the customer.city is the same. It is just a hypothetic example.



--

Sidney Lins
sidtj


Yii Smart Menu
Let YSM automagically hide/show your cmenu items according to result of checkAccess() function.

Yii Conditional Validator
Make validation using core validators based in conditions.


Posted ImageLista Yii Framework Brasil
A maior e mais ativa lista de discussão por e-mail em português.

Participe!
0

#4 User is offline   sidtj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 22-October 10

Posted 22 January 2012 - 09:02 AM

Documentation and examples were improved.

--

Sidney Lins
sidtj


Yii Smart Menu
Let YSM automagically hide/show your cmenu items according to result of checkAccess() function.

Yii Conditional Validator
Make validation using core validators based in conditions.


Posted ImageLista Yii Framework Brasil
A maior e mais ativa lista de discussão por e-mail em português.

Participe!
0

#5 User is offline   fantgeass 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 25-February 11

Posted 24 January 2012 - 03:26 PM

This extension useful for using filters. Thanks.
42
0

#6 User is offline   sidtj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 22-October 10

Posted 01 February 2012 - 11:04 PM

View Postfantgeass, on 24 January 2012 - 03:26 PM, said:

This extension useful for using filters. Thanks.


Hi fantgeass, there is totally new version, more easy, available.
Thanks for let me know you liked this extension!

--

Sidney Lins
sidtj


Yii Smart Menu
Let YSM automagically hide/show your cmenu items according to result of checkAccess() function.

Yii Conditional Validator
Make validation using core validators based in conditions.


Posted ImageLista Yii Framework Brasil
A maior e mais ativa lista de discussão por e-mail em português.

Participe!
0

#7 User is offline   sidtj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 22-October 10

Posted 01 February 2012 - 11:04 PM


New Version
  • Version 0.2.0
    • Usage made easier, more simplyfied and more objective;
    • New 'dot.notation' usage on attributes name (will be improoved on next versions);
    • Some bug fixes;


--

Sidney Lins
sidtj


Yii Smart Menu
Let YSM automagically hide/show your cmenu items according to result of checkAccess() function.

Yii Conditional Validator
Make validation using core validators based in conditions.


Posted ImageLista Yii Framework Brasil
A maior e mais ativa lista de discussão por e-mail em português.

Participe!
0

#8 User is offline   Favicon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 27-January 12
  • Location:UK

Posted 30 July 2012 - 09:58 AM

Hi, I love the concept but I'm having some trouble getting started with it. I'm trying to have three form fields (Date, Start and End) to be required when a fourth field (Ignore) is unchecked:

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'Ignore',
				'application.extensions.Validators.YiiConditionalValidator',
				'validation' => array(
					'compare',
					'compareValue' => false
					),
				'dependentValidations' => array(
					array('Day, Start, End', 'required'),
					)
				),
			
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID, CampaignID, Day, Start, End, DATEADD', 'safe', 'on'=>'search'),
		);
	}


The result is that I get the following error message:

Quote

array_slice() expects parameter 1 to be array, string given


Any idea what I've done wrong?

UPDATE: please forgive my laziness. After re-reading the examples more carefully, I have spotted my mistake ('required' needed to be inside another array) but I'm now having another issue:

Quote

PHP Fatal error: CModel::getValidators(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "YiiConditionalValidator" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /home/david/EagleEye/yii/framework/base/CModel.php on line 265


Never seen an error quite like this before! How can an object be "incomplete"?

This post has been edited by Favicon: 30 July 2012 - 10:28 AM

0

#9 User is offline   dpattayath 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 22-August 12

Posted 22 August 2012 - 11:25 PM

Hi Guys,

I had noticed one more important point when dealing with this validator. Since the dependentValidtions are done only when the conditional validation is true, the fields mentioned in dependentValidations are not considered as "safe". So we have to add one more rule to mark those fields as "safe". Actually this is a good thing and the framework is smart in handling this. Since am a newbie, just want to confirm what I found is correct or is there any other way we can normally achieve the same result?

Note: Also I am trying this in Tabular input form, so I have up to 5 rows of data for user to enter but not mandatory, user can enter none, one or two rows.

public function rules()
{
 return array (
	array ('customer','application.extensions.YiiConditionalValidator',
		'validation'=>array('required'),
		'dependentValidations'=>array(
		'account,rate'=>array(
			array('required', 'message'=>'{dependentAttribute} is required if the {attribute} specified is not empty.'),
		),
	)
	),
	array('account,rate','safe'),
	);
}

0

#10 User is offline   sidtj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 22-October 10

Posted 23 August 2012 - 09:46 AM

View Postdpattayath, on 22 August 2012 - 11:25 PM, said:

I had noticed one more important point when dealing with this validator. Since the dependentValidtions are done only when the conditional validation is true, the fields mentioned in dependentValidations are not considered as "safe". So we have to add one more rule to mark those fields as "safe".



dpattayath, you are right and that is a problem that I tryed to fix during so much much time but there are no way in Yii of to do those declared attributes to be set as 'safe' cause Yii does it in 'start-up' of the app.

As workaround I use to set a safe rule apart fot desired attributes:

array('attr1, attr2, ...', 'safe', 'on'=>'scenarioX'),

I have made some improvements in Yii-Conditional-Validator and soon I will publish the new version.

--

Sidney Lins
sidtj


Yii Smart Menu
Let YSM automagically hide/show your cmenu items according to result of checkAccess() function.

Yii Conditional Validator
Make validation using core validators based in conditions.


Posted ImageLista Yii Framework Brasil
A maior e mais ativa lista de discussão por e-mail em português.

Participe!
0

#11 User is offline   dpattayath 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 22-August 12

Posted 23 August 2012 - 07:29 PM

View Postsidtj, on 23 August 2012 - 09:46 AM, said:

dpattayath, you are right and that is a problem that I tryed to fix during so much much time but there are no way in Yii of to do those declared attributes to be set as 'safe' cause Yii does it in 'start-up' of the app.

As workaround I use to set a safe rule apart fot desired attributes:

array('attr1, attr2, ...', 'safe', 'on'=>'scenarioX'),

I have made some improvements in Yii-Conditional-Validator and soon I will publish the new version.



Cool. But still I think its a valid idea. Thinking from the perspective of dependent attributes as optional validations, its good to have the user aware of declaring it as safe, to stick with the rules of the framework.
0

#12 User is offline   sidtj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 22-October 10

Posted 27 September 2012 - 12:25 AM

Please, check the new version (1.0.0) on GitHub:

The extension page on yiiframework.com will be updated soon.

--

Sidney Lins
sidtj


Yii Smart Menu
Let YSM automagically hide/show your cmenu items according to result of checkAccess() function.

Yii Conditional Validator
Make validation using core validators based in conditions.


Posted ImageLista Yii Framework Brasil
A maior e mais ativa lista de discussão por e-mail em português.

Participe!
0

#13 User is offline   makmende 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 15-March 13

Posted 06 October 2013 - 02:53 AM

Thanks sidtj for the great extension...It seems exactly what am looking for. I can however not seem to have it work when using it in a form capturing related data.

Here are my rules
array('user_id', 'numerical', 'integerOnly'=>true),
array('event_id, campaign_id', 'length', 'max'=>7),
array('q1, q2, q3, q4, q5, q6, q7, q8, q9, q10', 'length', 'max'=>100),
array('q1', 'ext.YiiConditionalValidator',
'if' => array(
array('event.code_id', 'compare', 'compareValue'=>"303"),
),
'then' => array(
array('q1', 'required'),
),
),...
and here are the relations
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
'event' => array(self::BELONGS_TO, 'EventsAll', 'event_id'),
);
}
When I try to save I get the following error though...

AdhocVoc has not a relation named "event". Check the YiiConditionalValidator rule that is using the attribute name "event.code_id".
Where am i going wrong?
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