[Extension] Yii Conditional Validator Use core validation based on conditions
#1
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
#3
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.
#6
Posted 01 February 2012 - 11:04 PM
#7
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;
#8
Posted 30 July 2012 - 09:58 AM
/** * @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
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
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
#9
Posted 22 August 2012 - 11:25 PM
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'), ); }
#10
Posted 23 August 2012 - 09:46 AM
dpattayath, on 22 August 2012 - 11:25 PM, 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.
#11
Posted 23 August 2012 - 07:29 PM
sidtj, on 23 August 2012 - 09:46 AM, said:
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.
#12
Posted 27 September 2012 - 12:25 AM
The extension page on yiiframework.com will be updated soon.
#13
Posted 06 October 2013 - 02:53 AM
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?
#14
Posted 19 March 2015 - 07:51 AM
Model has not a relation named "service". Check the YiiConditionalValidator rule that is using the attribute name "service.name"
But right under validation rules i have:
public function relations()
{
return array(
'service' => array(self::BELONGS_TO, 'SocialServices', 'service_id')
);
}
Please advice with possible solution
#15
Posted 19 March 2015 - 11:31 AM