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:
- Download the XHTML validator from http://www.bermi.org/xhtml_validator and place it in the app/vendors directory
- Copy the code below to app/views/helpers/xhtml_validator.php
- 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;
}
}
}




Helpers like that is what I want :)
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.
This one of the coolest helpers I have see - thanks
Is there any chance this could be implemented using PHP’s built in HTML Tidy tool?
@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.
[...] 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) [...]
Absolutely great.
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 :)
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: 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
@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
@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…
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.
@Deena: Thanks for the link to htmLawed, I didn’t knew this tool.