Validation of data is a fundamentally important aspect of a CRUD application. You need to be sure that the data that the client is sending you is what you expect. This is not just for security reasons, protecting your data from misuse and intentional corruption, but also simply from unintentional mistakes in submitting data. Strings limited to a certain length, dates in a particular format and required fields are all common uses of data validation.
The Editor PHP libraries provide two different validation methods:
Field based, where each individual value submitted is independently validated: Field->validator().
Global validation, where the data submitted by the client-side can be validated as a whole: Editor->validator().
Field validation is the one you will most commonly work with - for example checking that an e-mail address field actually contains an e-mail address, and Editor provides a number of ready to use validators for the most common data types as well as the ability to specify your own. Global validation can be useful when checking dependencies between fields and conflicts in the existing data set.
Field validation
The Editor PHP libraries provide a validator() method for the Field class and a number of pre-built validation methods in the Validate class. Each of these validation methods returns a function which is executed when required to validate submitted data.
Consider the following example - the Validate::minNum() function is configured with a number and returns a function that can be used for the field validator.
Each validation method provided by the Validation class can optionally accept parameters to tell it how to validate data (for example the minLen method will accept an integer to indicate the minimum length of an acceptable string), but all optionally accept a ValidateOptions class instance. This class defines a number of options that are shared between all validation methods.
The ValidateOptions class is constructed with ValidateOptions::inst() (or new ValidateOptions() in PHP 5.4+) similar to the other Editor classes. It has three methods that can be used to alter the validation behaviour:
->allowEmpty( boolean ): How to handle empty data (i.e. a zero length string):
true (default) - Allow the input for the field to be zero length
false - Disallow zero length inputs
->message( string ): the error message to show if the validation fails. This is simply "Input not valid" by default, so you will likely want to customise this to suit your needs.
->optional( boolean ): Require the field to be submitted or not. This option can be particularly useful in Editor as Editor will not set a value for fields which have not been submitted - giving the ability to submit just a partial list of options.
true (default) - The field does not need to be be in the list of parameters sent by the client.
false - The field must be included in the data submitted by the client.
->message( 'Stock name must be at least 10 characters long')
) );
Multiple validators
It can often be useful to use multiple validators together, for example to confirm that an input string is less than a certain number of characters and also that it is unique in the database. Multiple validators can be added to a field simply by calling the Field->validator() method multiple times. Rather than overwriting the previous validator it will in fact add them together. They are run in the sequence they were added, and all validators must pass for the data to be accepted as valid.
As an example consider the following code which will check the min and max length of the data, and also that it is unique:
The Validate class in the Editor PHP libraries has a number of methods which can be used to perform validation very quickly and easily. These are:
Basic
none( ValidateOptions $cfg=null ) - No validation is performed
basic( ValidateOptions $cfg=null) - Basic validation - only the validation provided by ValidateOptions is performed
required( ValidateOptions $cfg=null) - The field must be submitted and the data must not be zero length. Note that Editor has the option of not submitting all fields (for example when inline editing), so the notEmpty() validator is recommended over this one.
notEmpty( ValidateOptions $cfg=null) - The field need not be submitted, but if it is, it cannot contain zero length data
boolean( ValidateOptions $cfg=null) - Check that boolean data was submitted (including 1, true on, yes, 0, false, off and no)
Numbers
numeric( string $decimalChar=".", ValidateOptions $cfg=null) - Check that any input is numeric.
minNum( integer $min, string $decimalChar=".", ValidateOptions $cfg=null) - Numeric input is greater than or equal to the given number
maxNum( integer $max, string $decimalChar=".", ValidateOptions $cfg=null) - Numeric input is less than or equal to the given number
minMaxNum( integer $min, integer $max, string $decimalChar=".", ValidateOptions $cfg=null) - Numeric input is within the given range (inclusive)
Strings
email( ValidateOptions $cfg=null) - Validate an input as an e-mail address.
ip( ValidateOptions $cfg=null) - Validate as an IP address.
minLen( integer $min, ValidateOptions $cfg=null) - Validate a string has a minimum length.
maxLen( integer $max, ValidateOptions $cfg=null) - Validate a string does not exceed a maximum length.
minMaxLen( integer $min, integer $max, ValidateOptions $cfg=null) - Validate a string has a given length in a range
noTags( ValidateOptions $cfg=null) - Don't allow HTML tags
url( ValidateOptions $cfg=null) - Validate as an URL address.
values(array $values, ValidateOptions $cfg=null) - Allow only values which have been specified in an array of options (the first parameter). This could be useful if you wish to have free-form input or event a select list, and want to confirm that the value submitted is within a given data set (see also the dbValues() method if valid values are stored in a database). Note that the values given in the values array are checked against the submitted data as case-sensitive data (i.e. `"A" != "a").
xss( ValidateOptions $cfg=null) - Check to see if the input could contain an XSS attack. This used the Field's XSS formatting function to determine if the input string needs to be formatted or not.
Date / time
dateFormat( string $format, ValidateOptions $cfg=null) - Check that a valid date input is given. The format is defined by PHP's date() method which is used for the parsing of date and time string.
Database
dbValues( ValidateOptions $cfg=null, string $column=null, string $table=null, Database $db=null, array $valid=null) - Allow only a value that is present in a database column. This is specifically designed for use with joined tables (i.e. ensure that the reference row is present before using it), but it could potentially also be used in other situations where referential integrity is required.
unique( ValidateOptions $cfg=null, string $column=null, string $table=null, array $db=null) - Ensure that the data submitted is unique in the table's column.
Custom field validators
If the provided methods above don't suit the kind of validation you are looking for, it is absolutely possible to provide custom validation methods. The validator() Field method will accept a function that returns either true or a string and accepts the following input parameters:
value - The value to be validated
data - The collection of data for the row in question
field -The host Field instance
host - Information about the host Editor instance
The return value should be true to indicate that the validate passed, or a string that contains an error message if the validation failed.
The following simple example shows how a maxLen string check could be implemented with a custom validation method:
Use the Validation.minNum() method to validate an input as numeric and greater or equal to a given number (no validation options specified, so the defaults are used):
You may also find it useful to be able to define a global validator that will execute whenever a request is made to the server and the Editor->process() method is executed. This method can be used to provide security access restrictions, validate input data as a whole or even to check that a user is logged in before processing the request.
Function
The function that is given to the Editor->validator() method has the following signature:
$editor - The Editor instance that the function is being executed for.
$action - The action being performed - this will be one of:
Editor::ACTION_READ - Read / get data
Editor::ACTION_CREATE - Create a new record
Editor::ACTION_EDIT - Edit existing data
Editor::ACTION_DELETE - Delete existing row(s)
Editor::ACTION_UPLOAD - Upload a file.
$data - The data submitted by the client.
The return value from the function should be a string if the validation fails, where the string returned is the error message to show the end user. If the validation passes, return an empty string, null or simply have no return statement.
Note that this function is executed only once when the Editor->process() method is called, rather than once per submitted row.
The PHP API developer documentation for the Editor PHP classes is available for detailed and technical discussion about the methods and classes discussed above.