Once again I'm digging into my mail bag:
Ray, I've recently been teaching myself and using model glue. For the most part i'm grasping it but one concept I can't quite grasp is validation. Pre my OOP enlightenment i would have a udf library (thanks CFLIB!) and i would include a particular function as needed. How would my implement this udf library in model glue? Ex, how do i validate email and ssn's from a form?
What I typically do, and I have seen others doing this as well, is to create a CFC to store your UDFs. Normally I like to create a CFC called utils. This will contain random little utilities that don't belong in their own CFC. An email checker is a good example of this. (Although don't forget that email and SSN checking is built into ColdFusion MX7.) So I'll have this utils.cfc with my functions.
Next - rememember that each controller will call an event, onRequestStart and onRequestEnd. Using onRequestStart, you can add to the event your utils library. Then in the views where you need the UDF library, simply grab it from the view state. Again, because you have it in the onRequestStart for your controller, all views will have access to it.
Archived Comments
Expanding on Ray's idea, you could always append a validation type to the end of your variable names, something like form.social_ssn or form.homephone_phone. Then write something in the onRequestStart to check the incoming variable names for _ssn, _email, _phone, and validate at that point.
Just a thought ;)
Justice I'd have to disagree with you. I don't think the onRequestStart should be doing random validations of stuff. That should only be done in specific handlers, like AddFooEvent or some such.
Ray,
Would you then have to write some type of event for each form that needed to be validated? It would just seem to me far easier to somehow format the variable name so that your code can catch any variable that needed validation and validate it auto-magically? If you had 6 different forms, would you trigger 6 different validation forms?
In case you wanted to do the same with Mach II, you can use Peter Farrell's udfsPlugin v1.2. It stored your UDFs so that they are available across your app.
Yes. I do write an event for each form, since each form is normally very unique.
Not to toot my own horn too much, but with ColdFusion on Wheels all you need to do is add a little declaration to the top of your model (which works for anything, not just emails):
<cfset validatesFormatOf("field","REGEX")>
Where "field" is the field in the model you want to look at and "REGEX" is a regular expression you want to match the field against. It handles the validation for you when you save the model and keeps track of any errors that occurred as a result of failing the validation. If you want to use the default error notices that Wheels provides, just add #errorMessagesFor("model")# to the top of your form (where "model" is whatever model you want to get error messages for) and it'll output a nice message telling you which fields failed validation and even highlight those fields in the form for you!
(There are a couple other validation methods as well: validatesUniquenessOf() makes sure there are no other records in the database with this same value, validatesPresenceOf() makes sure the field contains a value and validatesNumericalityOf() makes sure the value is a number.)
Sorry, couldn't resist. :)