[EXTENSION] ChildrenRequiredValidator
#1
Posted 09 August 2011 - 08:43 AM
http://www.yiiframew...uiredvalidator/
The validator is extremely simple, as I wanted to keep it as clean as possible. Complex parent field analysis can be performed by making a magic property of the model.
Any and all feedback/suggestions welcome!
Sr. Developer at Envisage International
D.H. Luther - Web Design & Development
Dana's Yii Blog
Extensions:
ChildrenRequiredValidator | ESitemap | EStrongPassword | ES3
#2
Posted 09 August 2011 - 08:55 AM
Sr. Developer at Envisage International
D.H. Luther - Web Design & Development
Dana's Yii Blog
Extensions:
ChildrenRequiredValidator | ESitemap | EStrongPassword | ES3
#3
Posted 10 August 2011 - 09:10 AM
I have a structure with three models -- one parent form with two children forms. Which child form is filled out depends on the toggle item selected from the parent.
Ie:
Form A:
Do you want to do B or C?
If B: Render and validate B
If C: Render and validate CSo, form A model has property B and property C.
I don't want A to validate unless B OR C validates and I want A to be smart enough to know what it needs to do.
So, in my rules for A, I add:
array( 'BValid', 'application.components.validators.ChildrenRequiredValidator', 'parentAttribute'=>'BOrC', 'requiredValue'=>true, 'match'=>self::OPTION_B ), array( 'CValid', 'application.components.validators.ChildrenRequiredValidator', 'parentAttribute'=>'BOrC', 'requiredValue'=>true, 'match'=>self::OPTION_C ),
And then I add the following methods to A:
public function getBValid(){ return $this->B->validate(); }
public function getCValid(){ return $this->C->validate(); }
So now, my controller will only have to run $a->validate() rather than a complex series to determine if A validates, then if B or C validates depending on which sub-section is required.
Like so in my controller action:
$model = new A();
if ( isset( $_POST['A'] ) )
{
$model->attributes = $_POST['A'];
if ( isset( $_POST['B'] ))
$model->B->attributes = $_POST['B'];
if ( isset( $_POST['C'] ))
$model->C->attributes = $_POST['C'];
// the children required way..
if ( $model->validate() )
{
// do whatever I need to do next here
}
}
Rather than the old way:
$model = new A();
if ( isset( $_POST['A'] ) )
{
$model->attributes = $_POST['A'];
if ( isset( $_POST['B'] ))
$model->B->attributes = $_POST['B'];
if ( isset( $_POST['C'] ))
$model->C->attributes = $_POST['C'];
// check which selected children have been chosen
// and ensure everything validates
if ( $model->validate() && (
$model->selectedB && $model->B->validate() ||
$model->selectedC && $model->C->validate()
))
{
// do whatever I need to do next here
}
}
The old way requries that my controller be very cognizant of the way B and C might be used and is much more confusing for another developer to maintain in the long run.
(Should be noted that I'm instancing B and C as part of the afterConstruct for A)
Sr. Developer at Envisage International
D.H. Luther - Web Design & Development
Dana's Yii Blog
Extensions:
ChildrenRequiredValidator | ESitemap | EStrongPassword | ES3
#4 Guest_Unedeengatlex_*
Posted 11 August 2011 - 01:50 AM
#5
Posted 11 August 2011 - 04:46 AM
Did someone post a question somewhere I missed about this extension?
Sr. Developer at Envisage International
D.H. Luther - Web Design & Development
Dana's Yii Blog
Extensions:
ChildrenRequiredValidator | ESitemap | EStrongPassword | ES3
#6
Posted 10 September 2011 - 09:29 PM
I'm trying to use this extenion with the function based validation, and it doesn't seem to be picking up the function. I have the following:
// Rules section
array('otherSuburb', 'application.components.validators.ChildrenRequiredValidator', 'parentAttribute' => 'isSuburbOther', 'requiredValue' => true, 'message' => 'Other Suburb cannot be blank.')
// Validator Function
public function isSuburbOther()
{
$suburbName = Lookup::item("customer", "suburbId", $this->suburbId);
if ($suburbName == 'Other') {
return true;
} else {
return false;
}
}
Error: Property "ContactForm.isSuburbOther" is not defined.
Highlighted line: if ( !$object->$parent || $object->$parent != $this->match )
Can you tell me what I've done wrong here?
#7
Posted 10 September 2011 - 09:35 PM
array('otherSuburb', 'application.components.validators.ChildrenRequiredValidator',
'parentAttribute' => 'suburbId',
'match' => Lookup::itemId("customer", "suburbId", "Other"),
'requiredValue' => true, 'message' => 'Other Suburb cannot be blank.'),
#8
Posted 10 September 2011 - 10:33 PM
Make sense?
Sr. Developer at Envisage International
D.H. Luther - Web Design & Development
Dana's Yii Blog
Extensions:
ChildrenRequiredValidator | ESitemap | EStrongPassword | ES3
#9
Posted 11 September 2011 - 01:51 AM
'parentAttribute' => 'getIsSuburbOther()'
'parentAttribute' => 'getIsSuburbOther'
function is declared as: public function getIsSuburbOther()
I continue to get the same error. The model I'm working on is based on CFormModel, as opposed to CActiveRecord. Would that make a difference?
#10
Posted 11 September 2011 - 08:29 AM
CBridgman, on 11 September 2011 - 01:51 AM, said:
'parentAttribute' => 'getIsSuburbOther()'
'parentAttribute' => 'getIsSuburbOther'
function is declared as: public function getIsSuburbOther()
I continue to get the same error. The model I'm working on is based on CFormModel, as opposed to CActiveRecord. Would that make a difference?
The method is getIsSuburbOther()
The parent attribute is isSuburbOther
It's standard "magic" for php and Yii for getting the result of a "get" method as a model property.
You're getting your names mixed up still.
Sr. Developer at Envisage International
D.H. Luther - Web Design & Development
Dana's Yii Blog
Extensions:
ChildrenRequiredValidator | ESitemap | EStrongPassword | ES3
#11
Posted 17 September 2011 - 03:37 AM
#12
Posted 05 October 2011 - 11:03 AM
Thank you
One question about the usage:
Is it possible to check against multiple matches?
E.g.:
array('parent',
'application.extensions.validators.ChildrenRequiredValidator',
'parentAttribute'=>'parent',
'match'=>'MULTIPLE VALUES HERE'),
#13
Posted 05 October 2011 - 11:15 AM
It could be refactored to check whether match is an array, and if so, match any value within it. But that would be a change.
If you open up the validator and look at line 43, that is the one that would need to be refactored.
Change:
if ( !$object->$parent || $object->$parent != $this->match) return;
to something like:
if ( !$object->$parent)
return;
if ( is_array( $this->match ) )
{
if ( !in_array( $object->parent, $this->match ))
return;
} else {
if ( $object->parent != $this->match )
return;
}
Then you can set match to be an array of allowed values.
I will update the extension to reflect this ability tonight.
Sr. Developer at Envisage International
D.H. Luther - Web Design & Development
Dana's Yii Blog
Extensions:
ChildrenRequiredValidator | ESitemap | EStrongPassword | ES3
#14
Posted 06 June 2012 - 03:29 AM
I have used this extension with my project , works wonderful . thanks
I have just one problem say example :
i have two fields min_age and max_age(min_age and max_age is not compulsory fields).Now when user fills the min_age then only max_age is compulsory(done by using this extension) and its works fine .But max_filed is showing me yii required mark by default(max_age*).
is there any way to remove this require mark.
Thanks:)
#15
Posted 06 June 2012 - 04:10 AM
mado023, on 06 June 2012 - 03:29 AM, said:
I have used this extension with my project , works wonderful . thanks
I have just one problem say example :
i have two fields min_age and max_age(min_age and max_age is not compulsory fields).Now when user fills the min_age then only max_age is compulsory(done by using this extension) and its works fine .But max_filed is showing me yii required mark by default(max_age*).
is there any way to remove this require mark.
Thanks:)
#16
Posted 06 June 2012 - 04:10 AM
Instead of using echo $form->labelEx($model,'max_age'); use echo $form->label($model,'max_age');
The above answer is based on the assumption that you are using default crud generated code from gii.

Help
















