Is there a better way of designing zend_forms rather than using decorators?

℡╲_俬逩灬. 提交于 2020-01-15 03:01:12

问题


I am currently using zend_decorators to add styles to my form. I was wondering if there is an alternative way of doing it? It is a bit difficult to write decorators. I would love the casual one using divs and css style :

<input type="submit" class="colorfulButton" > 

It is much simpler rather than set a decorator for a certain control and add it. Since it requires creating a decorator for each style implementation and adding it up with the control. Will view helpers to the trick?


回答1:


There's a few ways. You can roll your own element view helpers (which could get rather clumsy soon I guess).

Or... you could use a viewscript for the form, like this (very basic example):

class Your_Form extends Zend_Form
{
    public function init()
    {
        $this->setDecorators( array(
            'PrepareElements',
             array( 'ViewScript', array( 'viewScript' => 'path/to/viewscript.phtml' ) )
        ) );

        // only use basic decorators for elements
        $decorators = array(
            'ViewHelper',
            'Label',
            'Errors'
        );

        // create some element
        $someElement = new Zend_Form_Element_Text( 'someElement' );
        // set the basic decorators for this element and set a css class
        $someElement->setDecorators( $decorators )
                    ->setAttrib( 'class', 'someCssClass' );

        // add (potentially multiple) elements to this from
        $this->addElements( array(
            $someElement
        ) );

    }
}

See the standard decorators section about PrepareElements for why it's needed to have the PrepareElements decorator set for the form, when using the ViewScript decorator.

Then in the viewscript:

<?
    // the form is available to the viewscript as $this->element
    $form = $this->element;
?>
<!-- put whatever html you like in this script and render the basic element decorators seperately -->
<div>
   <? if( $form->someElement->hasErrors() ): ?>
   <?= $form->someElement->renderErrors() ?>
   <? endif; ?>
   <?= $form->someElement->renderLabel(); ?>
   <?= $form->someElement->renderViewHelper(); ?>
</div>



回答2:


If you just want to set a class attribute on a form element, there is no need to define a decorator : this can be done using some standard methods of zend_form's elements.

See the setAttrib() method, in the section Metadata and Attributes of the manual, and the example that's given there (quoting) :

// Equivalent to $element->setAttrib('class', 'text'):
$element->class = 'text;


And if you can set a class attribute this way, you can probably set it while constructing your form's elements, or in a .ini file that would define those elements -- there is an example that shows that a bit later in the page, in the Configuration section.



来源:https://stackoverflow.com/questions/2619593/is-there-a-better-way-of-designing-zend-forms-rather-than-using-decorators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!