问题
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