Validation methods for DataTables Editor fields.
These methods will typically be applied through the Field::validator
method and thus the arguments to be passed will be automatically resolved
by Editor.
The validation methods in this class all take three parameters:
- Data to be validated
- Full data from the form (this can be used with a custom validation method for dependent validation).
- Validation configuration options.
When using the Validate
class functions with the Field::validator
method, the second parameter passed into Field::validator is given
to the validation functions here as the third parameter. The first and
second parameters are automatically resolved by the DataTables\Editor\Field
class.
The validation configuration options is an array of options that can be used
to customise the validation - for example defining a date format for date
validation. Each validation method has the option of defining its own
validation options, but all validation methods provide four common options:
{boolean} optional
- Require the field to be submitted (false
) or not (true
- default). When set to true
the field does not need to be included in the list of parameters sent by the client - if set to false
then it must be included. This option can be be particularly used 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.
{boolean} empty
- Allow a field to be empty, i.e. a zero length string - ''
(true
- default) or require it to be non-zero length (false
).
{boolean} required
- Short-cut for optional=false
and empty=false
. Note that if this option is set the optional
and empty
parameters are automatically set and cannot be overridden by passing in different values.
{string} message
- Error message shown should validation fail. This provides complete control over the message shown to the end user, including internationalisation (i.e. to provide a translation that is not in the English language).
Namespace: DataTables\
Editor
Example:
Field::inst( 'engine' )->validator( 'Validate::required' )
Example:
Field::inst( 'reg_date' )->validator( 'Validate::notEmpty' )
Field::inst( 'reg_date' )->validator( 'Validate::basic', array('empty'=>true) )
Example:
Field::inst( 'reg_date' )->validator( 'Validate::date_format', 'D, d M y' )
Example:
Field::inst( 'reg_date' )->validator( 'Validate::date_format', array(
'format' => 'D, d M y',
'message' => "Invalid date"
) )
Example:
Field::inst( 'reg_date' )->validator( 'Validate::email', array('required'=>true) )
Field::inst( 'reg_date' )->validator( 'Validate::email', array(
'empty' => false,
'optional' => false
) )
Example:
Field::inst( 'engine' )->validator( function($val, $data, $opts) {
if ( ! preg_match( '/^1/', $val ) ) {
return "Value <b>must</b> start with a 1";
}
return true;
} )
Located at Editor/Validate.php
Methods summary
public static
true
|
#
none( string $val, string[] $data, array $opts, $host )
No validation - all inputs are valid.
No validation - all inputs are valid.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. No additional options are
available or required for this validation method.
- $host
Returns
true
|
public static
string|true
|
#
basic( string $val, string[] $data, array $opts, $host )
Basic validation - this is used to perform the validation provided by the
validation options only. If the validation options pass (e.g. required ,
empty and optional ) then the validation will pass regardless of the
actual value.
Basic validation - this is used to perform the validation provided by the
validation options only. If the validation options pass (e.g. required ,
empty and optional ) then the validation will pass regardless of the
actual value.
Note that there are two helper short-cut methods that provide the same
function as this method, but are slightly shorter:
// Required:
Validate::required()
// is the same as
Validate::basic( $val, $data, array( "required" => true
);
// Optional, but not empty if given:
Validate::notEmpty()
// is the same as
Validate::basic( $val, $data, array( "empty" => false
);
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. No additional options are
available or required for this validation method.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
required( string $val, string[] $data, array $opts, $host )
Required field - there must be a value and it must be a non-empty value
Required field - there must be a value and it must be a non-empty value
This is a helper short-cut method which is the same as:
Validate::basic( $val, $data, array( "required" => true
);
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. No additional options are
available or required for this validation method.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
notEmpty( string $val, string[] $data, array $opts, $host )
Optional field, but if given there must be a non-empty value
Optional field, but if given there must be a non-empty value
This is a helper short-cut method which is the same as:
Validate::basic( $val, $data, array( "empty" => false
);
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. No additional options are
available or required for this validation method.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
boolean( string $val, string[] $data, array $opts, $host )
Validate an input as a boolean value.
Validate an input as a boolean value.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. No additional options are
available or required for this validation method.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
numeric( string $val, string[] $data, array $opts, $host )
Check that any input is numeric.
Check that any input is numeric.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. No additional options are
available or required for this validation method.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
minNum( string $val, string[] $data, integer|array $opts, $host )
Check for a numeric input and that it is greater than a given value.
Check for a numeric input and that it is greater than a given value.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. The additional option of
min is available for this method to indicate the minimum value. If
only the default validation options are required, this parameter can
be given as an integer value, which will be used as the minimum value.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
maxNum( string $val, string[] $data, integer|array $opts, $host )
Check for a numeric input and that it is less than a given value.
Check for a numeric input and that it is less than a given value.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. The additional option of
max is available for this method to indicate the maximum value. If
only the default validation options are required, this parameter can
be given as an integer value, which will be used as the maximum value.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
minMaxNum( string $val, string[] $data, integer|array $opts, $host )
Check for a numeric input and that it is both greater and smaller than
given numbers.
Check for a numeric input and that it is both greater and smaller than
given numbers.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. The additional options of
min and max are available for this method to indicate the minimum
and maximum values, respectively.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
email( string $val, string[] $data, array $opts, $host )
Validate an input as an e-mail address.
Validate an input as an e-mail address.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. No additional options are
available or required for this validation method.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
minLen( string $val, string[] $data, integer|array $opts, $host )
Validate a string has a minimum length.
Validate a string has a minimum length.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. The additional option of
min is available for this method to indicate the minimum string
length. If only the default validation options are required, this
parameter can be given as an integer value, which will be used as the
minimum string length.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
maxLen( string $val, string[] $data, integer|array $opts, $host )
Validate a string does not exceed a maximum length.
Validate a string does not exceed a maximum length.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. The additional option of
max is available for this method to indicate the maximum string
length. If only the default validation options are required, this
parameter can be given as an integer value, which will be used as the
maximum string length.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
minMaxLen( string $val, string[] $data, integer|array $opts, $host )
Require a string with a certain minimum or maximum number of characters.
Require a string with a certain minimum or maximum number of characters.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. The additional options of
min and max are available for this method to indicate the minimum
and maximum string lengths, respectively.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
ip( string $val, string[] $data, array $opts, $host )
Validate as an IP address.
Validate as an IP address.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. No additional options are
available or required for this validation method.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
url( string $val, string[] $data, array $opts, $host )
Validate as an URL address.
Validate as an URL address.
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. No additional options are
available or required for this validation method.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
dateFormat( string $val, string[] $data, array|string $opts, $host )
Check that a valid date input is given
Check that a valid date input is given
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
If given as a string, then $opts is the date
format to check the validity of. If given as an array, then the
date format is in the 'format' parameter, and the return error
message in the 'message' parameter.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|
public static
string|true
|
#
unique( string $val, string[] $data, integer|array $opts, $host )
Check that the given value is unique in the database
Check that the given value is unique in the database
Parameters
- $val
- The value to check for validity
- $data
- The full data set submitted
- $opts
Validation options. The additional options of
db - database connection object, table - database table to use and
column - the column to check this value against as value, are also
available. These options are not required and if not given are
automatically derived from the Editor and Field instances.
- $host
Returns
string|true true if the value is valid, a string with an error
message otherwise.
|