How to add id attribute to Zend Framework form?

我怕爱的太早我们不能终老 提交于 2019-12-10 21:25:55

问题


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>

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/29612217/how-to-add-id-attribute-to-zend-framework-form

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