Custom Validator for PHP

The Custom Validator for PHP is a PHP class designed to submit HTML interfaces of dynamic web applications to online validation services.

See a demo. View the demo code


To use this class, just include it in your HTML page, instantiate the class, and invoke methods on it. A minimal configration is as follows:

$v = new Validator();

This code must appear at the top of the page, before any other content is generated.

This will result in the current page being submitted to the W3C validation service and the results displayed in a new window. The following options can be set on this class:

method signature default value description
the URL of the validator to submit this page to; place the string '###URL###' where the URL of the resource to be validated should go (as in the default)
setFileExtension(String); .validate.html this is extension added to the script name when the HTTP request is written to disk
setButtonText(String); validate this is the text which appears in the validate button
setButtonMarker(String); this asks the validator to substitute a string in the target page with the validation link, allowing you to avoid a sure-fire validation error (the validator drops the link at the end of the file by default) as well as customize the display
setTargetWindow(String) newWindow the name of the window to direct validation results to; you may use the typical constants, '_blank', '_top', etc.
setWindowOptions(String) scrollbars=yes,location=yes,
These are the options to use IF opening a browser window to display the results
setWorkDir(String) [file path to executing PHP script] the directory to write static HTML files to
setWorkDirUrl(String) [url of executing PHP script] the url of the workDir directory

A fuller configuration might look like:

$v = new Validator();

This asks the Validator class to...

Advanced Usage

If you want to place more than one validation link on an interface (for example, a link to both an HTML and an accessibility validation service), you need to configure the Validator object slightly differently due to the way that PHP output buffering works. Instead of setting parameters directly on the Validation object, you add a series of ValidationButtons to the Validation object. The syntax looks like this:

$v = new Validator();

$vb = new ValidatorButton();

$vb = new ValidatorButton();


The methods listed above for ValidatorButton behave the same as their counterparts in Validator, and all methods on Validator will continue to work as previously explained.

Known Issues

There is a problem with reading data via fgets() under certain server/PHP/character encoding configurations which causes bad characters to be written to the file and can result in validation issues. If you know what might cause this--or solve this--please contact me.