How to get rid of dl, dt, dd tags Zend_Form?

I would like to get rid of the format of my definition list Zend_Form. This is the layout I'm going to do:

<form>
    <p>
        <label for="email" class="required">Your email address:</label>
        <input type="text" name="email" id="email" value="">
    </p>
    <p>
        <input type="submit" name="submit" id="submit" value="Subscribe">
    </p>
    <input type="hidden" name="active" value="true" id="active">
    <input type="hidden" name="signupDate" value="" id="signupDate">
</form>

What do I need to do for my form to get this layout?

class Default_Form_Subscribe extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        $this->addElement('text', 'email', array(
            'label'      => 'Email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array('EmailAddress')
        ));

        $this->addElement('submit', 'submit', array(
            'label'    => 'Subscribe',
            'ignore'   => true
        ));

        $this->addElement('hidden', 'active', array(
            'value'=>'true'
        ));
        $this->addElement('hidden', 'signupDate', array(
            'value' => Zend_Date::now()->toString('YYYY-MM-dd')
        ));
    }
}
+3
source share
3 answers

Oh, they beat me ... I went with the approach to creating a custom definition that can be applied to certain elements. There was also reset decorators on the form itself, to remove the default wrapper 'dl', it seems to do exactly what you need:

class Default_Form_Subscribe extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        // reset form decorators to remove the 'dl' wrapper
        $this->setDecorators(array('FormElements','Form'));

        // custom decorator definition for form elements
        $customElementDecorators = array(
            'ViewHelper',
            'Errors',
            array(
                'Description',
                array('tag' => 'p','class' => 'description')
            ),
            array(
                'Label',
                array('separator' => ' ')
            ),
            array(
                array('data' => 'HtmlTag'),
                array('tag' => 'p')
            )
        );

        $this->addElement('text', 'email', array(
            'label'      => 'Email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array('EmailAddress'),
            'decorators' => $customElementDecorators
        ));

        $this->addElement('submit', 'submit', array(
            'label'    => 'Subscribe',
            'ignore'   => true,
            'decorators' => $customElementDecorators
        ));

        $this->addElement('hidden', 'active', array(
            'value'=>'true',
            'decorators' => array('ViewHelper')
        ));
        $this->addElement('hidden', 'signupDate', array(
            'value' => Zend_Date::now()->toString('YYYY-MM-dd'),
            'decorators' => array('ViewHelper')
        ));
    }
}
+11
source

Let me add, probably, a slightly shorter path that worked fine for me:

//after adding all the form elements
//all form elements in a loop
foreach ($this->getElements() as $el) {
    $el->setDecorators( 
        array('ViewHelper', 'Errors', array('HtmlTag', array('tag' => 'p') 
    );
}
//form itself
$this->setDecorators( array('FormElements', 'Form') );

, html

+2

You must customize the item decorators Zend_Form. Check out this tutorial .

In your case, it will be something similar to this:

$form->setElementDecorators(array(
    'ViewHelper',
    'Errors',
    array('Label', array('tag' => 'label', 'placement' => 'prepend'),
    array(array('data' => 'HtmlTag'), array('tag' => 'p')),
));

This sets decorators for all form elements. You can also customize individual elements (for example, hidden and buttons).

It is also possible the formation of display groups and their individual design.

+1
source

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


All Articles