How to style forms within Zend?

I really like the idea of ​​creating forms in a separate class that controls validation, etc., but I don’t like that everything ends in DL, and also cannot use square notation in post-elements of the type <input type='checkbox' name='data[]'>.

Is there any other way to generate forms - like in views, so that I can style them the way I want, but also preserve the aspect of validation? Also how can I load this view into the current view (using a partial view?)

+3
source share
4 answers

I would suggest switching to a full ViewScript form to control the display of form elements.

, , , , :

<?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'))
        ));  
    }
}

application/modules/default/views/myForm.phtml

, $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>

script -, , <?= $this->form; ?>

. ,

, ...

, .

+3
+2

, .

But if you prefer, you can manually create “templates” for your form using the “Image Decorator” .

0
source

Source: https://habr.com/ru/post/1782572/


All Articles