简体   繁体   中英

How to add id attribute to Zend Framework form?

I have simple class which extends Zend_Form with 2 elements:

class Application_Form_Player extends Zend_Form
{
    public function init()
    {
        $this->addElement('text', 'firstName', array(
                'label' => $this->getView()->translate('First name') . ' (' . $this->getView()->translate('imaginary') . ')',
                'required' => true,
                'filters' => array('StringTrim'),
                'validators' => array(array('StringLength', false, array(1, 256)))
            )
        );
        $this->addElement('text', 'lastName', array(
                'label' => $this->getView()->translate('Last name') . ' (' . $this->getView()->translate('imaginary') . ')',
                'required' => true,
                'filters' => array('StringTrim'),
                'validators' => array(array('StringLength', false, array(1, 256)))
            )
        );
    }

}

Is there a Zend_Form method that I can use to add "id" attribute to the HTML "dl" created by this class?

This is resulting HTML dl to which I want to add id attribute:

<dl class="zend_form">
...
</dl>

$this->setAttrib('id', 'someId'); , this would help you where $this being the object of your current class.

$element = new Zend_Form_Element_Text('something');

$element->setAttrib('id','myId')
->setLabel('myLabel')
->setRequired(true);

$this->addElements(array(
     $element
));

You can add validators and filters with addValidator() and addFilter() too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM