Reputation: 19207
This is my form: I want to make sure that AT LEAST one of the name/email pairs are filled in, so I'm working on a custom validation rule in my model.
alt text http://files.getdropbox.com/u/240752/formfields.png
I originally thought it would a simple case of making the first name/email pair mandatory, but this doesn't cover the other fields if someone fills in the third one for example, and omits the email.
Also, I only need to check for a valid email address if the corresponding friend field is filled in.
Am I overthinking this? I think I need some kind of interaction between the rules, but I'm stuck hard.
Upvotes: 2
Views: 2310
Reputation: 28205
How are your fields being generated? I assume you're using something like:
<?php echo $form->input('Friend.name'); ?> [html stuff] <?php echo $form->input('Friend.email'); ?>
Since doing that several times within a page will produce duplicate ids (like, "FriendName
" would be the resulting ID for each field generated by <?php echo $form->input('Friend.name'); ?>
), you're probably going to have to add a number to each field name as you're generating them and then loop over $this->data['Friend']
in your controller and invalidate the offending fields as you find them (if the name is present, but the email is not, as you say).
I don't think there's a built-in way for cake to handle a situation like this, but I've been wrong before!
Upvotes: 0
Reputation: 9964
I would write a custom validation method for this purpose: http://book.cakephp.org/view/150/Custom-Validation-Rules#Adding-your-own-Validation-Methods-152
Upvotes: 1
Reputation: 530
place your validation logic inside beforeValidate callback.
Upvotes: 0