Automatic XHTML validation

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;
        }
    }
}

6 Comments

  1. Posted July 5, 2006 at 5:28 pm | Permalink

    Helpers like that is what I want :)

  2. Posted July 5, 2006 at 9:16 pm | Permalink

    Man you have just like saved my life, I was needing something like this. If we ever met, remember me to buy you a beer.

  3. Posted July 6, 2006 at 5:37 am | Permalink

    This one of the coolest helpers I have see – thanks

  4. Posted July 6, 2006 at 9:33 am | Permalink

    Is there any chance this could be implemented using PHP’s built in HTML Tidy tool?

  5. Posted July 6, 2006 at 5:46 pm | Permalink

    @sosa: I will remember you ;) I am glad it is useful for you.

    @tariquesani: welcome.

    @richardathome: Yes, it should be possible to build such a helper with PHP’s HTML tidy. But the disadvantage of such an approach is that you will have to modify your PHP installation in order to use the helper.

  6. Posted July 16, 2006 at 8:08 pm | Permalink

    Absolutely great.


One Trackback/Pingback

  1. […] cake baker » Automatic XHTML validation cakebaker has produced a XHTML validation helper for CakePHP. Uses the XHTML validator from the Akelos framework. Wonder if he can get this working with PHP inbuilt HTMLTidy api? (tags: cakephp helper xhtml validate) […]

%d bloggers like this: