Validate
in package
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 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 totrue
the field does not need to be included in the list of parameters sent by the client - if set tofalse
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 foroptional=false
andempty=false
. Note that if this option is set theoptional
andempty
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).
Tags
Table of Contents
Methods
- basic() : string|true
- 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.
- boolean() : string|true
- Validate an input as a boolean value.
- dateFormat() : string|true
- Check that a valid date input is given.
- dbValues() : string|true
- Check that the given value is a value that is available in a database - i.e. a join primary key. This will attempt to automatically use the table name and value column from the field's `options` method (under the assumption that it will typically be used with a joined field), but the table and field can also be specified via the options.
- email() : string|true
- Validate an input as an e-mail address.
- fileExtensions() : mixed
- fileSize() : mixed
- ip() : string|true
- Validate as an IP address.
- maxLen() : string|true
- Validate a string does not exceed a maximum length.
- maxNum() : string|true
- Check for a numeric input and that it is less than a given value.
- minLen() : string|true
- Validate a string has a minimum length.
- minMaxLen() : string|true
- Require a string with a certain minimum or maximum number of characters.
- minMaxNum() : string|true
- Check for a numeric input and that it is both greater and smaller than given numbers.
- minNum() : string|true
- Check for a numeric input and that it is greater than a given value.
- mjoinMaxCount() : mixed
- mjoinMinCount() : mixed
- none() : callable
- No validation - all inputs are valid.
- noTags() : string|true
- Check if there are any tags in the submitted value.
- notEmpty() : callable
- Optional field, but if given there must be a non-empty value.
- numeric() : string|true
- Check that any input is numeric.
- required() : string|true
- Required field - there must be a value and it must be a non-empty value.
- unique() : string|true
- Check that the given value is unique in the database.
- url() : string|true
- Validate as an URL address.
- values() : string|true
- Confirm that the value submitted is in a list of allowable values.
- xss() : string|true
- Check if string could contain an XSS attack string.
Methods
basic()
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.
public
static basic([mixed $cfg = null ]) : string|true
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, [
"required" => true
];
// Optional, but not empty if given:
Validate::notEmpty()
// is the same as
Validate::basic( $val, $data, [
"empty" => false
];
Parameters
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
boolean()
Validate an input as a boolean value.
public
static boolean([mixed $cfg = null ]) : string|true
Parameters
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
dateFormat()
Check that a valid date input is given.
public
static dateFormat(mixed $format[, mixed $cfg = null ]) : string|true
Parameters
- $format : mixed
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
dbValues()
Check that the given value is a value that is available in a database - i.e. a join primary key. This will attempt to automatically use the table name and value column from the field's `options` method (under the assumption that it will typically be used with a joined field), but the table and field can also be specified via the options.
public
static dbValues([mixed $cfg = null ][, mixed $column = null ][, mixed $table = null ][, mixed $db = null ][, mixed $values = [] ]) : string|true
Parameters
- $cfg : mixed = null
- $column : mixed = null
- $table : mixed = null
- $db : mixed = null
- $values : mixed = []
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
email()
Validate an input as an e-mail address.
public
static email([mixed $cfg = null ]) : string|true
Parameters
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
fileExtensions()
public
static fileExtensions(mixed $extensions[, mixed $msg = 'This file type cannot be uploaded.' ]) : mixed
Parameters
- $extensions : mixed
- $msg : mixed = 'This file type cannot be uploaded.'
fileSize()
public
static fileSize(mixed $size[, mixed $msg = 'Uploaded file is too large.' ]) : mixed
Parameters
- $size : mixed
- $msg : mixed = 'Uploaded file is too large.'
ip()
Validate as an IP address.
public
static ip([mixed $cfg = null ]) : string|true
Parameters
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
maxLen()
Validate a string does not exceed a maximum length.
public
static maxLen(mixed $max[, mixed $cfg = null ]) : string|true
Parameters
- $max : mixed
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
maxNum()
Check for a numeric input and that it is less than a given value.
public
static maxNum(mixed $max[, mixed $decimal = '.' ][, mixed $cfg = null ]) : string|true
Parameters
- $max : mixed
- $decimal : mixed = '.'
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
minLen()
Validate a string has a minimum length.
public
static minLen(mixed $min[, mixed $cfg = null ]) : string|true
Parameters
- $min : mixed
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
minMaxLen()
Require a string with a certain minimum or maximum number of characters.
public
static minMaxLen(mixed $min, mixed $max[, mixed $cfg = null ]) : string|true
Parameters
- $min : mixed
- $max : mixed
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
minMaxNum()
Check for a numeric input and that it is both greater and smaller than given numbers.
public
static minMaxNum(mixed $min, mixed $max[, mixed $decimal = '.' ][, mixed $cfg = null ]) : string|true
Parameters
- $min : mixed
- $max : mixed
- $decimal : mixed = '.'
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
minNum()
Check for a numeric input and that it is greater than a given value.
public
static minNum(mixed $min[, mixed $decimal = '.' ][, mixed $cfg = null ]) : string|true
Parameters
- $min : mixed
- $decimal : mixed = '.'
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
mjoinMaxCount()
public
static mjoinMaxCount(mixed $count[, mixed $msg = 'Too many items.' ]) : mixed
Parameters
- $count : mixed
- $msg : mixed = 'Too many items.'
mjoinMinCount()
public
static mjoinMinCount(mixed $count[, mixed $msg = 'Too few items.' ]) : mixed
Parameters
- $count : mixed
- $msg : mixed = 'Too few items.'
none()
No validation - all inputs are valid.
public
static none() : callable
Return values
callable —Validation function
noTags()
Check if there are any tags in the submitted value.
public
static noTags([mixed $cfg = null ]) : string|true
Parameters
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
notEmpty()
Optional field, but if given there must be a non-empty value.
public
static notEmpty([ValidateOptions $cfg = null ]) : callable
This is a helper short-cut method which is the same as:
Validate::basic( $val, $data, [
"empty" => false
];
Parameters
- $cfg : ValidateOptions = null
-
Validation options
Return values
callable —Validation function
numeric()
Check that any input is numeric.
public
static numeric([mixed $decimal = '.' ][, mixed $cfg = null ]) : string|true
Parameters
- $decimal : mixed = '.'
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
required()
Required field - there must be a value and it must be a non-empty value.
public
static required([mixed $cfg = null ]) : string|true
This is a helper short-cut method which is the same as:
Validate::basic( $val, $data, [
"required" => true
];
Parameters
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
unique()
Check that the given value is unique in the database.
public
static unique([mixed $cfg = null ][, mixed $column = null ][, mixed $table = null ][, mixed $db = null ]) : string|true
Parameters
- $cfg : mixed = null
- $column : mixed = null
- $table : mixed = null
- $db : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
url()
Validate as an URL address.
public
static url([mixed $cfg = null ]) : string|true
Parameters
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
values()
Confirm that the value submitted is in a list of allowable values.
public
static values(mixed $values[, mixed $cfg = null ]) : string|true
Parameters
- $values : mixed
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.
xss()
Check if string could contain an XSS attack string.
public
static xss([mixed $cfg = null ]) : string|true
Parameters
- $cfg : mixed = null
Tags
Return values
string|true —true if the value is valid, a string with an error message otherwise.