2008年8月14日星期四

[fw-mvc] Forms rant - Zend_Form critique. Was: Model Validation

Adam, I hope you don't mind me getting this discussion back on the list.

I didn't want to tackle too many problems at once and was planning to comment on
validation only but I just kept writing. Sorry for a longish post :)

I haven't used Zend_Form (and I probably won't be using it in the future), so I can't
show you the exact solution. Zend_Form aside, you should come up with a scheme that
suits your problem. I would probably create an object (error model) or even an
associative array holding error messages that could be used at a view layer. The model
can throw exception or can provide some error handling Zend_Validate-like interface,
e.g. $obj->validate(), $obj->hasErrors(), $obj->getErrors().

Now, to the second problem. I, personally, don't like the design of Zend_Form as well as
Quickform's because I don't like the libraries that model html elements (text, textarea,
select, etc). This is relevant to form generators ONLY which I don't like to use. And I
don't see why the whole form has to be designed that way only to support automatic form
visualization.

When you think about it in detail, is it really relevant for your model from what type
of html control the data comes from? Is it relevant if search query comes from textarea,
text input or a hidden field? It's still a string representing a text and it comes
through GET or POST parameters. And it could as well come from an e-mail/IM provided
URL, PDF form, XForm, AJAX, etc. Basically you should be able to validate the data
without the form object.

Furthermore, when you model your form data as html elements you create two additional
problems - your view layer information leaks to the model (html text labels, form data
visual representation, etc) and you restrict yourself to html forms. And now try to
separate the view and controller layers by using two-step view and xslt. You're
basically screwed.

I have provided my thoughts on a form proposal like a year ago (which as it seems were
ignored). To reiterate - php form libraries should work on GET/POST data not on html
elements. Validators and validation should be form independent (as they validate data
and can validate more than just forms). Form generators should be able to use form data
and validators or just validation feedback. If you don't like generators, you should be
able to use html form view helpers which too should be able to use the same form and
validation data.

Now, when it comes to this design, Solar Form is not far from hitting the bullseye:

http://solarphp.org/manual:form_processing:overview

It's not the first time Paul M. Jones is doing something great. Just look at hist PEAR
Text_Wiki. And Zend_View also is based on his ideas. Hats off to you Paul.

But unfortunately Solar still mixes view layer and models - by using labels and types.
Yes, types. All GET/POST incoming form data is represented as strings - and this should
be filtered/validated at a model level.

I have been planning to create a Zend Framework compatible form library but I don't have
much time on my hands. And I have lost my imperative when Zend_Form got created ;)


Adam Jensen wrote:
> On Thu, Aug 14, 2008 at 12:24 PM, Michał Minicki <martel@post.pl> wrote:
>> 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.
>
> Very interesting point; I think I've been running up against this
> distinction myself lately.
>
> Let me add another wrinkle: we primarily use the validators in form
> objects for checking the acceptability of user input, correct?
> However, form objects only really handle the first kind of validation;
> as you said later on, only the model knows how to do the semantic
> validation.
>
> If that's the case, how do we validate user input semantically?
> Semantic validation errors needs to be communicated to the user just
> as surely as formatting validation errors do, and it'd be nice to
> handle them all in one place (ideally Zend_Form, since its
> error-reporting mechanisms are so easy to use).
>
> I suppose the Zend_Form subclass could just duplicate all the semantic
> validation rules that are already in the model, but I think we all
> know how difficult that would make our lives later on :)
>
> Thanks!
> Adam


--
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

没有评论: