Reputation: 9227
Note: Zend Framework 1.12
I'm trying to add support for some HTML5 form elements. I found some snippets online to start me off. Now I need to add support for the new "required" attribute:
<input type="email" name="email" id="email" required>
Ideally I should be able to just use
$element->setRequired(true);
and it would add the appropriate tag. But I can't work out how to access this setting from within the helper. So I'm having to add this to the form as well
$element->setAttrib('required', '');
That's not ideal. How can I make setRequired add the tag automatically? Here's the helper code so far:
<?php
class Application_View_Helper_FormEmail extends Zend_View_Helper_FormElement {
public function formEmail($name, $value = null, $attribs = null) {
$info = $this->_getInfo($name, $value, $attribs);
extract($info);
$disabled = '';
if ($disable) {
// disabled
$disabled = ' disabled="disabled"';
}
$xhtml = '<input type="email"'
. ' name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id) . '"'
. ' value="' . $this->view->escape($value) . '"'
. $disabled
. $this->_htmlAttribs($attribs)
. $this->getClosingBracket();
return $xhtml;
}
}
Upvotes: 1
Views: 107
Reputation: 9227
Ahh never mind, I managed to work it out! Just needed to override the setRequired method in the form element, and add the setAttrib call to it:
class Application_Form_Element_Email extends Zend_Form_Element_Xhtml {
/**
* Default form view helper to use for rendering
* @var string
*/
public $helper = 'formEmail';
public function __construct($spec, $options = null) {
parent::__construct($spec, $options);
$this->addValidator('Email');
}
public function setRequired($flag = true) {
parent::setRequired($flag);
parent::setAttrib('required', '');
return $this;
}
}
Upvotes: 1