2009年9月16日星期三

Re: [fw-mvc] ways of rendering forms

-- Seth Atkins <satkins@nortel.com> wrote
(on Wednesday, 16 September 2009, 01:33 PM -0500):
> Since someone recently raised the topic of Matthew's slides at the Dutch '09
> conference, I went and looked at those. There was a slide that had some
> approaches I haven't seen examples of. The slide (150) says:
>
> Recommendations
>
> -Either pass the form to the view, or pull it from the model within the view
>
>
> (Seth: I'm unfamiliar with the latter option. Anyone have a code example??)

In the examples I had, I was attaching forms to my service objects as
general purpose validators. As such, I could then do this in my
controller:

$this->view->foo = $foo; // where $foo is a service object

and then this in your view:

<?php
$form = $this->foo->getForm();

> -Configure decorators in the view, if customizations are needed (view logic)
>
>
> (Seth: I would assume related to the above, once "pulled" into the view,
> you can addDecorator(). Correct? Is this what is meant? An example would
> also be useful.)

Yes, you can call addDecorator() at any time. While you probably want to
define the decorators for the default use case within your form class,
you can always customize them in the view.

$this->form->addDecorator(...);
$this->form->getElement('bar')->addDecorator(...);

> -For truly complex markup, render individual decorators within the markup
>
>
> (Seth: I know the "how", meaning the syntax, for rendering an individual
> element, but not sure in the big picture what this option is describing. If
> I'm assigning decorators to elements, why do I need to explicitly call a
> render method? I thought it automatically rendered?)

So, one case I've envisioned is to only assign the ViewHelper, Label,
and Errors decorator to each element. This then allows you to create
some complex markup in your view:

<?php $this->form->setView($this); ?>
<form action="<?php $this->url(...) ?>" method="post">
<fieldset>
<legend>Demographics</legend>
<p>
Please provide the following demographic information for our
records:
</p>
<?php
$subForm = $this->form->getSubForm('demog');
foreach ($subForm->getElements() as $element): ?>
<div class="element">
<div class="input">
<?php echo $element->renderLabel() ?>
<?php echo $element->renderViewHelper() ?>
</div>
<?php echo $element->renderErrors() ?>
</div>
<?php endforeach ?>
</fieldset>
<fieldset>
...
</fieldset>

This makes for markup that's slightly more friendly for designers, while
simultaneously retaining many of the benefits of Zend_Form (scoping of
element names, retaining and escaping values, reporting validation
errors, etc.).

> This would make for a great tutorial (given the other recent thread
> highlighting the need for more tutorials) :-).

Already planned. :)

Most of the material has already been blogged:

http://weierophinney.net/matthew/plugin/tag/decorators

and I plan to adapt these entries for the tutorial section of the
manual.

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

没有评论: