Re: Validation rule with or
I finished a quick mockout of the code for my article. I am still adding the finishing touches, but you can get the idea. The one main change I hope to make by the time this gets read is to handle all the logic upfront, and only once, instead of for each field. So here it is.. Validating dependent fields in CakePHP... THe dreaded OR rule http://edwardawebb.com/programming/php-programming/cakephp/validating-optional-fields-cakephp-dreaded-scenario On Feb 11, 11:07 am, WebbedIT wrote: > Sounds like a more elegant approach than mine .. look forward to the > article :) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Validation rule with or
Sounds like a more elegant approach than mine .. look forward to the article :) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Validation rule with or
Henrik: Seems like a pretty common need doesn't it. The solution is pretty simple. You call the same validation rule for every related field. $validate= array( 'field1'=>array('rule'=>'dependentFields'), 'field2'=>array('rule'=>'dependentFields') ) Then the method function dependenFields() { $return=false; logic based on fields to do the comparison etc. // if dependencies pass $return=true; //otherwise unique errors $return="You provided your Company Name, but forgot Phone" } and those messages ()if your logic is right) will show only under the failing boxes. I will post the full details in an article on my site which is not quite complete (and I am at work right now :/). Check back tonight perhaps. } On Feb 10, 4:35 am, WebbedIT wrote: > I do not believe there are any built-in validation rules to do this so > you need to specify your own custom validation method such as: > > function __validateFieldDependancy($data, $field1, $field2) { > // return false if ThisModelName.field1 and ThisModelName.field2 are > empty > return empty($this->data[$this->name][$field1']) && empty($this->data > [$this->name][$field2]) ? false : true; > > } > > This would go in your model and would be called by the following > validation rule: > > var $validate = array( > 'field_name' => array( > 'rule' => array('__validateFieldDependancy', 'fieldName1', > 'fieldName2'), > 'message' => 'Your error message' > ) > ) > > Not sure if it is neccessary to make this method private (named > starting with double underscores) but thought it was more secure to do > so. > > There is probably a better way to test both fields are empty ... happy > for people to better educate me on this as I'm self-taught with PHP. > > If you are going to use this type of validation rule regularly across > multiple models it may be worth aking it available to all models by > placing it in /app/app_model.php > > Hope this helps, > > Paul (WebbedIT) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Validation rule with or
I do not believe there are any built-in validation rules to do this so you need to specify your own custom validation method such as: function __validateFieldDependancy($data, $field1, $field2) { // return false if ThisModelName.field1 and ThisModelName.field2 are empty return empty($this->data[$this->name][$field1']) && empty($this->data [$this->name][$field2]) ? false : true; } This would go in your model and would be called by the following validation rule: var $validate = array( 'field_name' => array( 'rule' => array('__validateFieldDependancy', 'fieldName1', 'fieldName2'), 'message' => 'Your error message' ) ) Not sure if it is neccessary to make this method private (named starting with double underscores) but thought it was more secure to do so. There is probably a better way to test both fields are empty ... happy for people to better educate me on this as I'm self-taught with PHP. If you are going to use this type of validation rule regularly across multiple models it may be worth aking it available to all models by placing it in /app/app_model.php Hope this helps, Paul (WebbedIT) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Validation rule with or
I have a form where you have to enter either ("firstname" AND "lastname" AND "phone") OR ("companyname" AND phone") how do I make a validate rule like that? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---