2008年8月14日星期四

Re: [fw-mvc] Model Validation

Matthew Weier O'Phinney wrote:

> One thing I've started playing with is attaching my forms to my models.
> They then serve two purposes: input filtering, and a GUI representation
> of the model as a form. Just another thing to play with. :)

Let me chime in for a sec. There are two kinds of validation - general/formatting/visual
and semantic validation. These two actually belong to different objects. Where do they
belong? The short answer is to form and model respectively.

The long answer. Let's get a date for example. The date is represented in specific
numbers and/or strings. We all know it has a year, month and day as well as that there
are leap years, different number of days in a month, etc. This is a general validation
every model that handles any date could use. In order not to repeat oneself this should
be used as a service in forms and server components because a number of different models
may need to use exactly the same kind of validation. Zend_Validate is a good use of such
very generic validators.

Then we have a semantic validation. The same date represents something - a birthday, a
date of software release, a date of a ship log event, etc. Now, when it comes to
validation, if you store a birthday in your model (i.e. user object) then you know the
date can't be set in the future, so you validate that in the model. This kind of
validation should occur in the model as only the model knows what his internal data
represents.

This is a very straightforward example and semantic validation could be a lot more
complicated (it can check if end date is later than start date or if you can start the
service due to some flag telling to hold all operations, check a blacklist, etc).

But keep in mind this is only my own experience speaking. I don't have any links singed
with famed names to back this argument up.

--
Michał Minicki aka Martel Valgoerad | martel@aie.pl | http://aie.pl/martel.asc
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"Idleness is not doing nothing. Idleness is being free to do anything." --
Floyd Dell

没有评论: