Repeating models in a single form: how to validate?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Repeating models in a single form: how to validate?

flemieux

Hi,

maybe this has been answered before, and I couldn't find the answer in
my extensive search (or I was not looking for the right ting), but I am
having a hard time figuring out a way to do this in Cake elegantly.

Basically, I have to do a very simple "Send to a friend" form, where a
sender (name, email) has the ability to send an email to up to "n"
receivers (name, email). The sender has the ability to "Add" boxes to
fill in more emails.

I'm trying to do this using existing helpers and model structure (I use
html helper for form rendering), and I have 2 models (Sender and
Receiver), but I can't seem to find how I could "validate" the form,
save everything in the DB, and send the appropriate email given the
structure of the platform.

Will I have to program the whole thing by workarounds and such, or has
anyone ever found a clever solution to this common problem?

Enlighten me, tell me I oversaw something :D

Thanks,

flemieux--


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Repeating models in a single form: how to validate?

Jose da Silva-2

I think you should exemplify your doubt, i'd like to help you bu i
can't understand your problem.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Repeating models in a single form: how to validate?

Grant Cox
In reply to this post by flemieux

In your form, you can have model and field names that don't actually
correspond to your actual model files, or database fields.  So, in your
form you could have:

$html->input("Sender/name")
$html->input("Sender/email")

$html->input("Receiver1/name")
$html->input("Receiver1/email")

...

$html->input("Receiver2/name")
$html->input("Receiver2/email")


then in your controller, loop over the form data (
$this->data['Reciever1'] will be set ), and validate each email address
inside the controller.  Use the same regular expression as the model
validation would :

if ( preg_match(VALID_EMAIL, $this->data['Reciever1']['email']) )

Any fields that fail validation, set a message.  It would be good to
use the Model invalidate() function, but I don't think it work with
these made up model names (Receiver1 etc).  Anyway, if there are no bad
email addresses, then save the sender and receiver objects.  These may
still fail validation and not save, as you may have additional Model
validation.  Saving is really easy, just pass the appropriate part of
the data array

if ( $this->Receiver->save( $this->data['Receiver1'] ) )


Hope that helps.


On Oct 31, 5:51 am, "flemieux" <[hidden email]> wrote:

> Hi,
>
> maybe this has been answered before, and I couldn't find the answer in
> my extensive search (or I was not looking for the right ting), but I am
> having a hard time figuring out a way to do this in Cake elegantly.
>
> Basically, I have to do a very simple "Send to a friend" form, where a
> sender (name, email) has the ability to send an email to up to "n"
> receivers (name, email). The sender has the ability to "Add" boxes to
> fill in more emails.
>
> I'm trying to do this using existing helpers and model structure (I use
> html helper for form rendering), and I have 2 models (Sender and
> Receiver), but I can't seem to find how I could "validate" the form,
> save everything in the DB, and send the appropriate email given the
> structure of the platform.
>
> Will I have to program the whole thing by workarounds and such, or has
> anyone ever found a clever solution to this common problem?
>
> Enlighten me, tell me I oversaw something :D
>
> Thanks,
>
> flemieux--


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---