Top.Mail.Ru
? ?

cakephp

validating models without a database table

Aug. 4th, 2006 | 06:39 pm
posted by: Imagebobalien in Imagecakephp

occasionally, elements of my cake apps call for forms to be validated that aren't linked to a database table - for instance a Contact form that emails the form to a certain email address, as opposed to logging contacts in a database. You would probably want to be sure that the user has filled in all of the necessary information before creating and sending the email.

moreCollapse )

Link | Leave a comment {10} | | Flag

cakephp

another validation approach?

Jun. 14th, 2006 | 01:04 pm
posted by: Imagebobalien in Imagecakephp

yes... another... and this one I believe is actually Cake-"recommended"

up until now I've been using the following:
http://wiki.cakephp.org/tutorials:complex_model_validation_routines

This method is not officially cake-supported, but I liked the flexibility it gave in customizing error messages for invalid fields.

Here's another method I tinkered with at some point too: http://cakebaker.wordpress.com/2006/02/06/yet-another-data-validation-approach/

Both were nice and flexible, but a little over-complicated, and not extremely "strict MVC".

Gwoo has recently turned me on to the Model::invalidate() method, which is new to 1.0 I believe, and after a bit of playing last night, it seems to work quite well, and is extremely simple to implement.

You can try and piece together some information about it here and here

In the above google groups link there's an expanded method where someone hacked the invalidates() function to also accept error messages (so the messages could be passed to the error display, instead of residing in the view) however, I could not get the hacked way to work properly. The basic invalidates() work very well tho, and plays nicely with the simple, traditional Cake validation method of having a $validate array in your model, something the other complex validation methods override.

Another point of note is that you can invalidate fields that aren't in your model (such as 'username_taken' or 'image_filesize') and display these errors accordingly in your view. Also, this method plays nice with the regular $html->tagErrorMsg() and doesn't require any additional helpers or edits to app_model.

here's some codeCollapse )

Link | Leave a comment | | Flag

Image