Top Tip: XHTML with Zend Form Elements

When you render a Zend_Form, the elements will render to HTML compliance rather than XHTML compliance, even if you have < ?php echo $this->doctype('XHTML1_STRICT');?> at the top of your layout script. Practically, this means that all the input elements do not end in "/>".

To resolve this, you need to call the doctype() view helper prior to rendering your form.

Within my projects, I do this within a front controller plug-in called ViewSetup that looks a little like this:


class App_Controller_Plugin_ViewSetup extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
// setup the layout
Zend_Layout::startMvc();

$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->init();

$view = $viewRenderer->view;
$view->doctype('XHTML1_TRANSITIONAL');

$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
}
}

Obviously, this class lives in the file library/App/Controller/Plugin/ViewSetup.php.

As you can see, I also set up the meta tag for the content-type to UTF-8 ready for rendering later.

9 Responses to “Top Tip: XHTML with Zend Form Elements”

  1. 1 Chad Kieffer

    Very helpful indeed! Thanks for sharing. By the way, it appears that you have a mismatched single quote type in appendName().

    I think this ′was meant to be this '

  2. 2 Rob...

    Hi Chad,

    That'll be WordPress trying to be helpful! It's correct when I edit the entry.

    Regards,

    Rob...

  3. 3 Christian

    Rob,
    I may be wrong(wouldn't be the first time) but for 'Content-Type' shouldn't you be using $view->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8')?

    Christian

  4. 4 Rob...

    Christian,

    You are right :) I've updated.

    Regards,

    Rob...

  5. 5 Nour

    Hallo Rob, I want to use fckeditor in mijn zf Wesite. Can you help me how i can do that.

    Regards,
    Nour

  6. 6 Gonzalo

    Hi Rob!, i think this is a real good wordk, but i don`t know hot to implement into my proyect, i have an modular estructure, something like this,

    public/
    library/
    aplications/
          layouts/
          config/
          modules/
                 contact/
                        controllers/                             
                                 plugins/
                        views/
                        models/
    

    shout i call it
    Aplications_Modules_Contact_Controller_Plugin_ViewSetup

    and that's it? Regardss, Gonzalo

  7. 7 Rob...

    Gonzalo,

    I would create a directory under library/ called "App" and store things like this in there.

    Regards,

    Rob...

  8. 8 Jon

    The Zend docs say that XHTML11 is an option, but in fact it's not in the current 1.5.2 release.

    http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.doctype

    XHTML11 is an option in the trunk code, so it will probably make it into the next release.

  9. 9 Julian

    Thanks for posting this. It helped solve my problem with special chars not displaying correctly.

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.