To ensure I write valid XHTML code I wrote a simple helper which does automatically validate the generated code. The helper uses the XHTML validator from the Akelos framework. This validator is labelled as “experimental”, so it is possible that some errors are not found. Maybe there exists a better XHTML validator somewhere else?

The usage is simple:

  1. Download the XHTML validator from http://www.bermi.org/xhtml_validator and place it in the app/vendors directory
  2. Copy the code below to app/views/helpers/xhtml_validator.php
  3. Add the helper to the helpers array of your controller(s): var $helpers = array(’XhtmlValidator’);
// app/views/helpers/xhtml_validator.php
vendor('XhtmlValidator');

class XhtmlValidatorHelper extends Helper
{
    function afterRender()
    {
        if (DEBUG > 0)
        {
            $html = @ob_get_clean();
            $originalHtml = $html;
            ob_start();

            $XhtmlValidator = new XhtmlValidator();

            if($XhtmlValidator->validate($html) === false)
            {
                echo 'Ooops! There are some errors on the XHTML page';
                $XhtmlValidator->showErrors();
            }

            echo $originalHtml;
        }
    }
}