Yii Framework Forum: [EXTENSION] ChildrenRequiredValidator - Yii Framework Forum

Jump to content

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

[EXTENSION] ChildrenRequiredValidator Rate Topic: -----

#1 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 09 August 2011 - 08:43 AM

This thread is for any discussion related to the ChildrenRequiredValidator.

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!
1

#2 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 09 August 2011 - 08:55 AM

Haven't tested it, but anonymous function to the match field *should* ™ work as well.
0

#3 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 10 August 2011 - 09:10 AM

Just thought I'd share this additional method for using the validator.

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 C


So, 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)
0

#4 Guest_Unedeengatlex_*

  • Group: Guests

Posted 11 August 2011 - 01:50 AM

You should post all question regarding user made extensions in the original topic of the extension, so that the builder will read as they might not always read the topics posted in this section...
0

#5 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 11 August 2011 - 04:46 AM

This is the original thread and those are additional examples, not questions.

Did someone post a question somewhere I missed about this extension?
0

#6 User is offline   CBridgman 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 26-June 11

Posted 10 September 2011 - 09:29 PM

Hi,

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?
0

#7 User is offline   CBridgman 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 26-June 11

Posted 10 September 2011 - 09:35 PM

Ok, I worked around the issue (sample below), but I'd still like to know if I did something wrong, or if there's a bug in the validator?

array('otherSuburb', 'application.components.validators.ChildrenRequiredValidator',
      'parentAttribute' => 'suburbId',
      'match' => Lookup::itemId("customer", "suburbId", "Other"),
      'requiredValue' => true, 'message' => 'Other Suburb cannot be blank.'),

0

#8 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 10 September 2011 - 10:33 PM

Your first way would have worked, but to access it magically, you needed to call the method "getIsSuburbOther()" which would allow your model to access it as a property $model->isSuburbOther

Make sense?
0

#9 User is offline   CBridgman 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 26-June 11

Posted 11 September 2011 - 01:51 AM

Hmm, unfortunately no. I tried:
'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?
0

#10 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 11 September 2011 - 08:29 AM

View PostCBridgman, on 11 September 2011 - 01:51 AM, said:

Hmm, unfortunately no. I tried:
'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.
0

#11 User is offline   CBridgman 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 26-June 11

Posted 17 September 2011 - 03:37 AM

Awesome, it works now. Didn't realise I needed to leave the 'get' bit off in the function reference. Thanks.
0

#12 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 05 October 2011 - 11:03 AM

Great extension!

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'),

0

#13 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 203
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 05 October 2011 - 11:15 AM

No, not with the current implementation.

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.
1

#14 User is offline   mado023 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-June 12

Posted 06 June 2012 - 03:29 AM

Hi,
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:)
0

#15 User is offline   mado023 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-June 12

Posted 06 June 2012 - 04:10 AM

View Postmado023, on 06 June 2012 - 03:29 AM, said:

Hi,
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:)

0

#16 User is offline   mado023 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-June 12

Posted 06 June 2012 - 04:10 AM

hey got an answer :
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.
:)
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