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 <[email protected]> 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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to