yvoyer
yvoyer

Reputation: 7516

Zend_Form output input elements without "/>" tag

When creating this form with the Zend Framework, I want the output to validate with the doctype strict, but it failed because the inputs don't have an end tag "/>".

How can I make it pass validation ?

The form:

require_once "Zend/Form.php";
require_once "Zend/Form/Element/Text.php";
require_once "Zend/Form/Element/Submit.php";

class Form_Test extends Zend_Form
{
    public function init()
    {
        // Email
        $email = new Zend_Form_Element_Text('email');
        $email->setRequired(true)
            ->addValidator('EmailAddress')
            ->setLabel("Email Address");

            // Submit
            $submit = new Zend_Form_Element_Submit('login');
            $submit->setLabel('Login');

            // Add element to form.
            $this->addElements(array(
                $email,
                $submit
            ));

            $this->setLegend('Login');
            $this->setElementDecorators(array(
                'ViewHelper',
                array('Label', array('separator' => '', 'requiredPrefix' => '* ')),
                array('HtmlTag', array('tag' => 'p', 'class' => 'form-element')),
            ));

            // Buttons do not need labels
            $submit->setDecorators(array(
                array('ViewHelper'),
                array('HtmlTag', array('tag' => 'p', 'class' => 'submit-button'))
            ));

            $this->getDecorator('HtmlTag')->setOption('tag', 'div');
            $this->getDecorator('HtmlTag')->setOption('class', 'form');
        }
    }

The html output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr-CA" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Form test</title>
</head>
<body>
    <form enctype="application/x-www-form-urlencoded" action="" method="post">
    <div>
        <p class="form-element">
            <label for="email" class="required">* Email Address</label>
            <input type="text" name="email" id="email" value="">
        </p>
        <p class="submit-button">
            <input type="submit" name="login" id="login" value="Login">
        </p>
    </div>
    </form>
</body>
</html>

Upvotes: 1

Views: 1274

Answers (1)

Darryl E. Clarke
Darryl E. Clarke

Reputation: 7637

You have to set the doctype of your view to something that requires self closing tags on individual elements.

In your Bootstrap you can do something like this:

protected function _initView() {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();
        $view->doctype('XHTML1_STRICT');
}

For all acceptable doctypes: http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.doctype

Also, if you're using this you might want to use the doctype header in your layout if you are not, that way it will keep things in order.

Upvotes: 3

Related Questions