2008年12月17日星期三

Re: [fw-mvc] Example Model review and best practices feedback request

-- Lars Strojny <lars@strojny.net> wrote
(on Wednesday, 17 December 2008, 02:29 PM +0100):
> Am Mittwoch, den 17.12.2008, 06:53 -0500 schrieb Matthew Weier
> O'Phinney:
> [...]
> > Correct, as well as an accessor for retrieving the form:
> >
> > public function getBugForm()
> > {
> > if (null === $this->_bugForm) {
> > $this->_bugForm = new Module_Form_Bug();
> > }
> > return $this->_bugForm;
> > }
>
> Hm, I see.
>
> > The basic technique is like this:
> >
> > public function save(array $data)
> > {
> > $validator = $this->getBugForm();
> > if (!$validator->isValid($data)) {
> > return false;
> > }
> > $values = $validator->getValues();
> >
> > // ...
> > }
>
> The nice thing is, you don't duplicate validation.

That was exactly my impetus to play with this method.

> What I would however prefer (and we built something like that) to put
> the validation and filtering into the model and find a way how the
> model can inform the form about errors. In this way the form would
> know about the domain model, but not the other way around, which is
> not really necessary. But that's harder to implement as it sounds. So
> what would be cool is some way to pass the validation layer from a
> model to a form. Something like FormFactory::createForm(Validatable
> $model). The Validatable interface declares a method like
> "getValidators()" or "getRules()" and these rules are attached to the
> form. This way a) you don't couple form and model, b) you don't repeat
> validation/filtering and c) you keep validation and filtering in the
> model.
> [...]

Well, the way I look at it is that the _form_ is the domain model's
validation/filtering mechanism... and just happens to be rendererable.

The form class defines basic decoration, but most view-specific setup of
the form happens in the view script. For example:

<?php
$form = $this->model->getBugForm();
$form->setMethod('post')
->setAction($this->url(
array(
'controller' => 'bug',
'action' => 'save',
),
'default',
true
))
->setDecorators(...);
echo $form;

I think the idea you present makes sense, but we already have this
functionality all in one place, so I figure, "why not use it?" It's
simply a matter of manipulating the two different domains it serves in
the appropriate locations.

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: