Hi Zoe
There are a number of ways to solve this problem. The neatest would be
to manually execute the validation prior to saving any of the data.
Have a look at the validation section of the Cake Manual. BTW - I only
use Cake 1.1.X, so don't know if this has changed at all for 1.2
In 1.1.X it looks like this (copied from the manual):
First, set the data to the model:
$this->ModelName->set( $this->data );
$this->ModelName->set( $this->data );
Then, to check if the data validates, use the validates method of the
model, which will return true if it validates and false if it doesn't:
if ($this->ModelName->validates()) {
// it validated logic
} else {
// didn't validate logic
}
If it validates, then save the data ...
As noted above I can think of a number of other ways of doing this, so
don't take the above as your only option. Hope this helps.
Regards,
Langdon
MonkeyGirl wrote:
> Hi.
>
> I'm about to try and write a bit of code that, based on data from one
> form, will insert some of the form's data into a record in one model,
> and the rest into a related record in another model.
>
> Before you point me at these threads:
> http://groups.google.co.uk/group/cake-php/browse_thread/thread/16bb0c2aedb1cb52/bfc625acd4d5b35b
> http://groups.google.co.uk/group/cake-php/browse_thread/thread/753022a670fd82e6/
> http://groups.google.co.uk/group/cake-php/browse_thread/thread/226e6aa1b443fe39/
>
> ...I should point out that the key word here is inserting, not
> updating. What I see as a potential problem is if the first model gets
> its new record OK, it gets a new insert ID just fine, the system tries
> to insert a record into the related model with that new ID for its
> foreign key... and that model's part of the form doesn't validate for
> some reason (say, a duplicate slug for example). It would throw up an
> error, the user would submit the form again, and then the code would
> try to insert the first record into the first model again (that would
> definitely cause a duplicate slug), causing another error.
>
> Is there a way around this, while sticking with a single form? Can I
> check if the second model's data would hypothetically validate without
> actually saving it into the database, before inserting the first
> model's data?
>
> I've had another look at the old manual:
> http://web.archive.org/web/20070823102844/http://manual.cakephp.org/chapter/models
> http://web.archive.org/web/20070812214744/manual.cakephp.org/chapter/validation
>
> ...but I can't seem to find anything about validating (as in checking
> all the validation rules and trying my beforeValidate code) without
> saving. It sounds like something that would probably exist, but I
> can't find a reference to it.
>
> (Sorry about the long winded way of asking the question, but often
> when I explain what I'm trying to do overall, someone will point out a
> much simpler way of doing it without having to try to do the specific
> thing I'm asking about.)
>
> Thank you all very much,
> Zoe.
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---