2009年9月14日星期一

Re: [fw-mvc] re[fw-mvc] nder Zend_Form in view

-- PHPScriptor <contact@phpscriptor.com> wrote
(on Monday, 14 September 2009, 12:31 PM -0700):
>
> I now we have decorators, but they are not so flexible that's way I want to
> control the form in a view. I can get the label, the errors, ... But I just
> can't find a way to get the input-fields
>
> echo $this->view->myfield->getLabel();
>
> foreach($this->view->myfield->getMessages() as $error :
> echo $error;
> endforeach;
>
> ...
>
> but I can't get <input name="myfield" ...> without the label, errormessages,

The Zend_Form_Element classes don't actually store the HTML markup for
the input fields; that work is delegated to view helpers in most cases.

This leaves You with several options. You can render just the ViewHelper
decorator, which will invoke the appropriate view helper for that
element and render just the HTML input element, with the appropriate
metadata filled in. There's a special short syntax for rendering
individual decorators, render<decoratorname>(), that comes in handy for
situations like this:

echo $this->form->myfield->renderViewHelper();

Alternately, you can grab the metadata you need and build it yourself:

<?php $element = $this->form->myfield; ?>
<input name="myfield" type="text" size="20"
value="<?php echo $this->escape($element->getValue()) ?>" />

My personal preference is the first option.

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

没有评论: