Works perfectly, thanks.

On Jul 13, 10:07 pm, Jeremy Burns | Class Outfit
<[email protected]> wrote:
> Here's a custom validation rule that works:
>
> function hasOnePhone() {
>
>         $addressTypes = array(
>                 'OrderAddress',
>                 'BillingAddress',
>                 'Customer'
>         );
>
>         foreach ($addressTypes as $addressType):
>
>                 if (isset($this->data[$addressType])):
>
>                         if (
>                                 $this->data[$addressType]['phone_home'] != 
> null
>                                 || $this->data[$addressType]['phone_work'] != 
> null
>                                 || $this->data[$addressType]['phone_cell'] != 
> null
>                                 ):
>                                 return true;
>                         endif;
>
>                 endif;
>
>         endforeach;
>
>         return false;
>
> }
>
> ...called like this in you $validate array:
>
> 'phone_home' => array(
>                         ...other rules...,
>                         'hasOnePhone' => array(
>                                 'rule' => 'hasOnePhone',
>                                 'message' => 'You must enter at least one 
> phone number.',
>                                 'last' => true
>                         ),
>                         ...
> ),
> 'phone_work' => array(
>                         ...other rules...,
>                         'hasOnePhone' => array(
>                                 'rule' => 'hasOnePhone',
>                                 'message' => 'You must enter at least one 
> phone number.',
>                                 'last' => true
>                         ),
>                         ...
> ),
>
> ...and so on.
>
> This actually checks across three models in the data array. You can take out 
> the [$addressType] foreach loop and simply hard code the name of your model.
>
> Jeremy Burns
> Class Outfit
>
> http://www.classoutfit.com
>
> On 13 Jul 2011, at 10:58, Sanfly wrote:
>
>
>
>
>
>
>
> > Hi
>
> > Im doing some data validation from a form using my model.
>
> > I have a form with phone numbers added: phone_home, phone_mobile,
> > phone_work
>
> > With my model, I want to validate that at least one of the three is
> > filled, but doesnt have to be all three.  Is there any way to do that?
>
> > --
> > Our newest site for the community: CakePHP Video 
> > Tutorialshttp://tv.cakephp.org
> > Check out the new CakePHP Questions sitehttp://ask.cakephp.organd help 
> > others with their CakePHP related questions.
>
> > To unsubscribe from this group, send email to
> > [email protected] For more options, visit this group 
> > athttp://groups.google.com/group/cake-php

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to