PHP and jQuery Validation combined.

This is my PHP validation class, it works together with the excellent jQuery Validation plugin to make validating forms simple.

By writing just one configuration file and using it for both the server-side validation (in PHP) and also for the client-side validation (in jQuery), you can reduce your code duplication, saving you time and keeping your code uncluttered.

As well as utilising the validation methods that are part of the jQuery plugin, the validation class utilises PHP's in built filtering functions giving validations methods for URL's, IP addresses and Emails. These are easily extended by writing custom validation methods, several of which have been included with the class such as validations for alphanumeric strings, date formats and strings containing HTML. 

Download

Click here to start the downloadFile Name: KP-Validation-vCurrent.zip
Version: 1.1 (26/11/2012)
File Size: 744kb
Contains: KP-Validation class, Working example, jQuery Core, jQuery Validation Plugin

How it works

Consider this really basic example of the validation for a newsletter subscription form, the form has just two fields Name and Email address. Using a fairly traditional approach to server side validation, your code may look something like this .....

<?php
function createData($name, $email){
	if( !is_string($name) || strlen($name) < 5 || strlen($name) > 100 ){
		return FALSE;
	}
	if( !is_string($email) || !filter_var($email, FILTER_VALIDATE_EMAIL) ){
		return FALSE;
	}
	// do something
}
?>

 Add to this a function to update the same data (omitted for berevity), and we have effectively doubled the amount of validation already. Now, if we add some jQuery validation to that form...

$(document).ready(function() {
	$("#newsletterForm").validate({
		rules: {
			name: {
				required: true, 
				minlength: 5, 
				maxlength: 150 
			}, 
			email: {
				required: true 
				email: true 
			}
		}, 
		messages: {
			name: {
				required: 'Name is required', 
				minlength: 'Please enter a name LONGER than the minimum of {0} characters', 
				maxlength: 'Please enter a name SHORTER than the maximum of {0} characters' 
			}, 
			email: {
				required: 'An email address must be provided' 
				email: 'Email format is not valid' 
			} 
		} 
	});
});

 ... we end up with three times the amount of repetition, across two different files.

 By using the KP-Validation class, all the validations are kept in one place, and only defined once in order to recude this repetition and also to keep in line with the DRY principle of software design. 

 Here's the config for the same two-field form (notice how similar it is to the jQuery validation config above)...

<?php
final class validation_newsletterForm extends validatorConfig {

	public function __construct() {

		$this->formId = 'newsletterForm';

		$this->sets = array(
			'CREATE' => array('name', 'email'),
			'UPDATE' => array('name', 'email'),
		};

		$this->rules = array(
			'name' => array(
				'fieldName' => 'name',
				'rules' => array(
					'required' => true,
					'MINLENGTH' => 5,
					'MAXLENGTH' => 100,
				),
				'messages' => array(
					'required' => 'Name is required',
					'MINLENGTH' => 'Please enter a name LONGER than the minimum of {0} characters',
					'MAXLENGTH' => 'Please enter a name SHORTER than the maximum of {0} characters',
				),
			),
			'email' => array(
				'fieldName' => 'email',
				'rules' => array(
					'required' => true,
					'email' => true,
				),
				'messages' => array(
					'required' => 'An email address must be provided',
					'email' => 'Email format is not valid',
				),
			),
		);
	}

}
?>

To use this config within the body of our PHP application we simply call the config and pass it to the validator, so no matter how many validations you have for the function it only ever takes up a couple of lines in your code...

<?php
public function create($arrData) {
	$valConfig = new validation_newsletterForm();
	if ( !$this->validator->validateConfig($valConfig, $arrData, 'CREATE') ) {
		return FALSE;
	}
	// do something
}
?>

 What about the client side validations? As above it's pretty straight forward, just call the config again and this time pass it to the Javascript writer, which will output your JavaScript code.

<?php
$vc = new validation_newsletterForm();
$vw = new validatorJsWriter();
echo $vw->write($vc);
?>

That's pretty much alll there is to it.

There's also a fully working example in the download.

Version History

VersionRelease DateComments
v1.1 26/11/2012 Changed the validationType interface so that the jQuery methods arent tied into using the PHP method names.
Added postcode validationType.
v1.0 08/11/2012 Initial release.

License

The boring bit, GPL v3 basically, click the logo for more info.

 GNU GPL v3 Logo

By Karl Payne

Comments

There are no comments, don't be shy, somebody has to be first.

Leave a comment