bpiec
bpiec

Reputation: 1631

Zend Forms email element changing to text

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?

Upvotes: 3

Views: 1363

Answers (1)

drew010
drew010

Reputation: 69937

You will need to implement your own view helper for rendering the email form element as well.

Zend_Form_Element_Text uses the formText view 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:

  • Remove the ViewHelper decorator from the element and use the ViewScript helper to render the element.
  • Make your own view helper formEmail almost identical to formText except that it outputs type="email"; and set the public $helper property in your Element class to formEmail. You will have to register the path to the formEmail helper using Zend_View::addHelperPath() so it will be found by the ViewHelper decorator.

Upvotes: 3

Related Questions