Automatic XHTML validation

Published on July 05, 2006 and tagged with cakephp  helper  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;
        }
    }
}

14 comments baked

  • rossoft July 05, 2006 at 17:28

    Helpers like that is what I want :)

  • sosa July 05, 2006 at 21:16

    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.

  • tariquesani July 06, 2006 at 05:37

    This one of the coolest helpers I have see – thanks

  • richardathome July 06, 2006 at 09:33

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

  • cakebaker July 06, 2006 at 17:46

    @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.

  • Richard@Home » Blog Archive » links for 2006-07-07 July 07, 2006 at 06:23

    [...] 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) [...]

  • AD7six July 16, 2006 at 20:08

    Absolutely great.

  • Nikita October 06, 2007 at 13:30

    Can anybody explain, why this tool don`t work on my site?
    I`ve done all of this
    >> 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’);

    At my config of the CakePHP, DEBUG constant is set to “1″ value.

    But I`ve never ever see the message “Ooops! There are some errors on the XHTML page”.

    Can anybody explain me, why it happens?

    P.S. Sorry for my bad English :)

  • Nikita October 06, 2007 at 13:32

    Can anybody explain, why this tool don`t work on

    my site?
    I`ve done all of this
    >> 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’);

    At my config of the CakePHP, DEBUG constant is

    set to “1″ value.

    But I`ve never ever see the message “Ooops! There

    are some errors on the XHTML page”.

    Can anybody explain me, why it happens?

    P.S. Sorry for my bad English :)

  • cakebaker October 06, 2007 at 17:31

    @Nikita: Well, it is possible that not all errors are detected. Add for example the tag “<table>” to your HTML and it should show you an error (at least it does it here). I don’t know which Cake version you use, but with the latest version from the repository I had to replace “DEBUG” with “Configure::read(‘debug’)”.

    HTH

  • Nikita October 07, 2007 at 10:19

    @cakebaker: Thanks. I have found, what mistake I`ve made.
    It was wrong vendor`s name. I simply have renamed vendors script name to “XhtmlValidator.php” and everything works fine now.

    I have a suggestion for you, guys. Have some of you ever been used a Tidy Tool(http://tidy.sourceforge.net/) for the purpose of automated XHTML code validation. I just had a litle glance on it. It looks like more advanced and developed tool, than this.
    But at the moment I like Xhtml Validator a lot.

    P.S. Sorry for my English

  • cakebaker October 08, 2007 at 16:36

    @Nikita: Cool to hear that it works now!

    Well, I considered to use the tidy tool, but as you see at http://ch2.php.net/manual/en/function.tidy-is-xhtml.php the necessary function is not yet implemented…

  • Deena Seth January 20, 2008 at 10:09

    Those who can’t or don’t want to use, may want to consider htmLawed which appears to be almost as good as HTML Tidy. Though it doesn’t do beautification, it has other features like anti-spam, anti-XSS, etc.

  • cakebaker January 23, 2008 at 18:38

    @Deena: Thanks for the link to htmLawed, I didn’t knew this tool.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License