A custom validation rule would do the job nicely
http://book.cakephp.org/view/150/Custom-Validation-Rules
Place all your logic in the model and it will be called before a save
and return meaningful errors to the form if the data is not valid
along with any other errors.
I have a message model which does something very similar, the user
specifies multiple recipients by a comma seperated list of usernames
in a text field (as you would with email addresses in outlook) and my
validation rule is as follows:
function __validateRecipients($field) {
if(!$field['recipients']) {
return "This field cannot be left blank";
} else {
$recipients = explode(',', str_replace(' ', '',
$field['recipients']));
foreach($recipients AS $k => $v) {
if(!$this->User->findByUsername($v)) {
$invalid[] = $v;
}
}
if(!empty($invalid)) {
return "Could not find recipient(s) ".join(', ', $invalid);
}
}
return true;
}
HTH
Paul
On Mar 8, 6:14 pm, "[email protected]"
<[email protected]> wrote:
> Hi
> I have a question about saving data.
> I describe my situation.
> I have a web application where a user can send document to other
> users.
> Now a user can send only to certain users.
> A user send a document through an "add" action where I do a save where
> the data to save is :
>
> $this->data['Send']['sender_id']
> $this->data['Recipient'][0]['recipient_id']
> $this->data['Recipient'][1]['recipient_id']
> $this->data['Recipient'][2]['recipient_id']
> .....
>
> Now I must control that the $this->data['Send']['recipient_id'] is one
> of a user that can receive and not another.
> My question is what is the best pattern to do the control that the
> $this->data['Send']['recipient_id'] is one of a user that can receive
> and not another.
> Do I it through validation (but I have to do a query for each
> $this->data['Recipient'][0]['recipient_id']) or I use another system?
>
> Many Thanks
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
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