开发者

How to style forms in the Zend framework?

开发者 https://www.devze.com 2023-02-01 20:31 出处:网络
I really like the idea of putting forms into a seperate class that manages validation etc, but I don\'t like everything ending up in a DL and also not being able to use square bracket notation in post

I really like the idea of putting forms into a seperate class that manages validation etc, but I don't like everything ending up in a DL and also not being able to use square bracket notation in post elements like <input type='checkbox' name='data[]'>.

Is there another way of generating forms - like in views so I开发者_如何转开发 can style them the way I want, but also keeping the validation aspect? Also how would I load this view into my current view (using partial view somehow?)


I would suggest going with the full out form ViewScript to manage how your form elements are displayed.

For example, if you wanted them displayed as list items you would have a form that look like this

<?php
class Default_Form_Myform extends Zend_Form
{
    public function  __construct($options = null) {
        parent::__construct($options);
    }
    public function  init() {
        parent::init();
        $this->setName('myform');
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name')
                    ->setDescription('Give your name...');
        $this->addElement($name);            
        $submit = new Zend_Form_Element_Submit('submit');
        $this->addElement($submit);        
        $this->clearDecorators();
        $this->setElementDecorators(
                array(
                    'viewHelper',
                    'Errors',
                    array('Label', array('class' => 'delabel')),
                    'Description',
                    array('HtmlTag', array('tag' => 'li')),        
                )
        );           
        // The template is at application/modules/default/views/myForm.phtml
        $this->setDecorators(array(
        array('ViewScript', array('viewScript' => 'myForm.phtml'))
        ));  
    }
}

Then you would have a template at application/modules/default/views/myForm.phtml

All elements are called by their declared name like $this->element->name

<form action="<?= $this->element->getAction(); ?>"
      method="<?= $this->element->getMethod(); ?>"
      enctype="<?= $this->element->getEnctype(); ?>"
      name="<?= $this->element->getName(); ?>">
    <ul>
    <?= $this->element->name; ?>
    <?= $this->element->submit ?>
    </ul>
</form>

Now in your view script you just need to echo the form like you normally would <?= $this->form; ?>

Note I'm using modules, you may or may not have that in the directories and class names

Please consider accepting answers to your questions...

it makes it worth while for those that answer.


You are looking for a form "decorator". Check out this tutorial: http://devzone.zend.com/article/3450

EDIT: Link dead as of June 2015. Looks like link moved here


You can use array notation with Zend_Form : http://framework.zend.com/manual/en/zend.form.advanced.html

You can remove the DL by setting your own decorators.


Decorators are awesome once you get the hang of them.

But if you prefer, you can manually create "templates" for your form using the ViewScript Decorator.

0

精彩评论

暂无评论...
验证码 换一张
取 消