Editor PHP 2.0.5

Validate

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:

  1. Data to be validated
  2. Full data from the form (this can be used with a custom validation method for dependent validation).
  3. 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 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).
Tags
example

// Ensure that a non-empty value is given for a field Field::inst( 'engine' )->validator( Validate::required() )


@example
 // Don't require a field to be submitted, but if it is submitted, it
 // must be non-empty
 Field::inst( 'reg_date' )->validator( Validate::notEmpty() )

@example
 // Date validation
 Field::inst( 'reg_date' )->validator( Validate::dateFormat( 'D, d M y' ) )

@example
 // Date validation with a custom error message
 Field::inst( 'reg_date' )->validator( Validate::dateFormat( 'D, d M y',
     ValidateOptions::inst()
         ->message( 'Invalid date' )
 ) )

@example
 // Require a non-empty e-mail address
 Field::inst( 'reg_date' )->validator( Validate::email( ValidateOptions::inst()
   ->empty( false )
 ) )

@example
 // Custom validation - closure
 Field::inst( 'engine' )->validator( function($val, $data, $opts) {
    if ( ! preg_match( '/^1/', $val ) ) {
      return "Value <b>must</b> start with a 1";
    }
    return true;
 } )

Table of Contents

_common()  : mixed
Perform common validation using the configuration parameters
_commonLegacy()  : mixed
Convert the old style validation parameters into ValidateOptions
_extend()  : mixed
Extend the options from the user function and the validation function with core defaults.
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()  : mixed
Validate an input as a boolean value.
dateFormat()  : mixed
Check that a valid date input is given
dbValues()  : mixed
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()  : mixed
Validate an input as an e-mail address.
fileExtensions()  : mixed
fileSize()  : mixed
ip()  : mixed
Validate as an IP address.
maxLen()  : mixed
Validate a string does not exceed a maximum length.
maxNum()  : mixed
Check for a numeric input and that it is less than a given value.
minLen()  : mixed
Validate a string has a minimum length.
minMaxLen()  : mixed
Require a string with a certain minimum or maximum number of characters.
minMaxNum()  : mixed
Check for a numeric input and that it is both greater and smaller than given numbers.
minNum()  : mixed
Check for a numeric input and that it is greater than a given value.
mjoinMaxCount()  : mixed
mjoinMinCount()  : mixed
none()  : mixed
No validation - all inputs are valid.
noTags()  : mixed
Check if there are any tags in the submitted value
notEmpty()  : mixed
Optional field, but if given there must be a non-empty value
numeric()  : mixed
Check that any input is numeric.
required()  : mixed
Required field - there must be a value and it must be a non-empty value
unique()  : mixed
Check that the given value is unique in the database
url()  : mixed
Validate as an URL address.
values()  : mixed
Confirm that the value submitted is in a list of allowable values
xss()  : mixed
Check if string could contain an XSS attack string

Methods

_common()

Perform common validation using the configuration parameters

public static _common(mixed $val, mixed $opts) : mixed

@internal

Parameters
$val : mixed
$opts : mixed
Return values
mixed

_commonLegacy()

Convert the old style validation parameters into ValidateOptions

public static _commonLegacy(mixed $cfg) : mixed

@internal

Parameters
$cfg : mixed
Return values
mixed

_extend()

Extend the options from the user function and the validation function with core defaults.

public static _extend(mixed $userOpts, mixed $prop, mixed $fnOpts) : mixed

@internal

Parameters
$userOpts : mixed
$prop : mixed
$fnOpts : mixed
Return values
mixed

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, array(
  "required" => true
);
// Optional, but not empty if given:
Validate::notEmpty()

// is the same as
Validate::basic( $val, $data, array(
  "empty" => false
);
Parameters
$cfg : mixed = null
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 ]) : mixed

@param string $val The value to check for validity

Parameters
$cfg : mixed = null
Return values
mixed

dateFormat()

Check that a valid date input is given

public static dateFormat(mixed $format[, mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$format : mixed
$cfg : mixed = null
Return values
mixed

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 = array() ]) : mixed

@param string $val The value to check for validity

Parameters
$cfg : mixed = null
$column : mixed = null
$table : mixed = null
$db : mixed = null
$values : mixed = array()
Return values
mixed

email()

Validate an input as an e-mail address.

public static email([mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$cfg : mixed = null
Return values
mixed

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."
Return values
mixed

fileSize()

public static fileSize(mixed $size[, mixed $msg = "Uploaded file is too large." ]) : mixed
Parameters
$size : mixed
$msg : mixed = "Uploaded file is too large."
Return values
mixed

ip()

Validate as an IP address.

public static ip([mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$cfg : mixed = null
Return values
mixed

maxLen()

Validate a string does not exceed a maximum length.

public static maxLen(mixed $max[, mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$max : mixed
$cfg : mixed = null
Return values
mixed

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 ]) : mixed

@param string $val The value to check for validity

Parameters
$max : mixed
$decimal : mixed = "."
$cfg : mixed = null
Return values
mixed

minLen()

Validate a string has a minimum length.

public static minLen(mixed $min[, mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$min : mixed
$cfg : mixed = null
Return values
mixed

minMaxLen()

Require a string with a certain minimum or maximum number of characters.

public static minMaxLen(mixed $min, mixed $max[, mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$min : mixed
$max : mixed
$cfg : mixed = null
Return values
mixed

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 ]) : mixed

@param string $val The value to check for validity

Parameters
$min : mixed
$max : mixed
$decimal : mixed = '.'
$cfg : mixed = null
Return values
mixed

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 ]) : mixed

@param string $val The value to check for validity

Parameters
$min : mixed
$decimal : mixed = "."
$cfg : mixed = null
Return values
mixed

mjoinMaxCount()

public static mjoinMaxCount(mixed $count[, mixed $msg = "Too many items." ]) : mixed
Parameters
$count : mixed
$msg : mixed = "Too many items."
Return values
mixed

mjoinMinCount()

public static mjoinMinCount(mixed $count[, mixed $msg = "Too few items." ]) : mixed
Parameters
$count : mixed
$msg : mixed = "Too few items."
Return values
mixed

none()

No validation - all inputs are valid.

public static none() : mixed

@return callable Validation function

Return values
mixed

noTags()

Check if there are any tags in the submitted value

public static noTags([mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$cfg : mixed = null
Return values
mixed

notEmpty()

Optional field, but if given there must be a non-empty value

public static notEmpty([ValidateOptions $cfg = null ]) : mixed

This is a helper short-cut method which is the same as:

Validate::basic( $val, $data, array(
  "empty" => false
);
Parameters
$cfg : ValidateOptions = null

Validation options @return callable Validation function

Return values
mixed

numeric()

Check that any input is numeric.

public static numeric([mixed $decimal = "." ][, mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$decimal : mixed = "."
$cfg : mixed = null
Return values
mixed

required()

Required field - there must be a value and it must be a non-empty value

public static required([mixed $cfg = null ]) : mixed

This is a helper short-cut method which is the same as:

Validate::basic( $val, $data, array(
  "required" => true
);
Parameters
$cfg : mixed = null
Return values
mixed

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 ]) : mixed

@param string $val The value to check for validity

Parameters
$cfg : mixed = null
$column : mixed = null
$table : mixed = null
$db : mixed = null
Return values
mixed

url()

Validate as an URL address.

public static url([mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$cfg : mixed = null
Return values
mixed

values()

Confirm that the value submitted is in a list of allowable values

public static values(mixed $values[, mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$values : mixed
$cfg : mixed = null
Return values
mixed

xss()

Check if string could contain an XSS attack string

public static xss([mixed $cfg = null ]) : mixed

@param string $val The value to check for validity

Parameters
$cfg : mixed = null
Return values
mixed

Search results