2008年12月17日星期三

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

Hi Matthew,

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. 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.
[...]

cu, Lars

没有评论: