I’m trying to create an input of type email in Zend Forms but I cannot accomplish the desired result.
I’ve created a custom class:
namespace AAA\Forms\Elements;
class Email extends \Zend_Form_Element_Text
{
function __construct($name, $label, $required)
{
parent::__construct($name);
$this->setAttrib('type', 'email');
$this->setLabel($label)->setRequired($required);
}
}
And then I’m using it like this:
class Application_Form_Register extends Zend_Form
{
public function init()
{
// …
$email = new AAA\Forms\Elements\Email('email', 'E-mail:', true);
$this->addElement($email);
// …
}
}
And what I get is:
<input type="text" name="email" id="email" value="" type="email">
Note the two type parameters.
I have set HTML5 doctype ($documentType = new Zend_View_Helper_Doctype(); $documentType->doctype('HTML5'); in Bootstrap.php) and I have also tried this:
function __construct($name, $label, $required)
{
$options = array();
$options['type'] = 'email';
parent::__construct($name, $options);
// …
}
I’ve read Zend HTML5 Form element types but none of the answers work. I’ve tried also Glitch library but the result is the same.
Anyone knows how to force Zend Framework to use HTML5 form controls?
You will need to implement your own view helper for rendering the email form element as well.
Zend_Form_Element_Textuses theformTextview helper (Zend/View/Helper/FormText.php) to render the HTML input and it is hard coded to output<input type="text"when rendering the element.Two possible ways to handle this would be to:
ViewHelperdecorator from the element and use theViewScripthelper to render the element.formEmailalmost identical toformTextexcept that it outputstype="email"; and set the public$helperproperty in your Element class toformEmail. You will have to register the path to theformEmailhelper usingZend_View::addHelperPath()so it will be found by theViewHelperdecorator.