Menu

## Validate
in package

Validate.php : 98

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() : 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

#### _common()

Validate.php : 171

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()

Validate.php : 201

Convert the old style validation parameters into ValidateOptions.

`public static _commonLegacy(mixed $cfg) : mixed`

@internal

##### Parameters

$cfg : mixed

##### Return values

mixed —

#### _extend()

Validate.php : 139

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()

Validate.php : 282

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

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  array $opts Validation options. No additional options are available or required for this validation method.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### boolean()

Validate.php : 371

Validate an input as a boolean value.

`public static boolean([mixed $cfg = null ]) : string|true`

##### Parameters

$cfg : mixed = null

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  array $opts Validation options. No additional options are available or required for this validation method.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### dateFormat()

Validate.php : 916

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

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  array|string $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.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### dbValues()

Validate.php : 1026

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() ]) : string|true`

##### Parameters

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

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $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.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### email()

Validate.php : 595

Validate an input as an e-mail address.

`public static email([mixed $cfg = null ]) : string|true`

##### Parameters

$cfg : mixed = null

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  array $opts Validation options. No additional options are available or required for this validation method.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### fileExtensions()

Validate.php : 1089
`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()

Validate.php : 1104
`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.php : 746

Validate as an IP address.

`public static ip([mixed $cfg = null ]) : string|true`

##### Parameters

$cfg : mixed = null

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  array $opts Validation options. No additional options are available or required for this validation method.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### maxLen()

Validate.php : 667

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

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $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.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### maxNum()

Validate.php : 495

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

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $opts Validation options.

  - `max`: 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.
  - `decimal`: is available to indicate what character should be used as the decimal
callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### minLen()

Validate.php : 629

Validate a string has a minimum length.

`public static minLen(mixed $min[, mixed $cfg = null ]) : string|true`

##### Parameters

$min : mixed
$cfg : mixed = null

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $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.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### minMaxLen()

Validate.php : 703

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

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $opts Validation options. The additional options of `min` and `max` are available for this method to indicate the minimum and maximum string lengths, respectively.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### minMaxNum()

Validate.php : 541

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

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $opts Validation options. Additional options:

  - `min`: indicate the minimum value.
  - `max`: indicate the maximum value.
  - `decimal`: is available to indicate what character should be used as the decimal
callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### minNum()

Validate.php : 449

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

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $opts Validation options. Additional options:

  - `min`: 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.
  - `decimal`: is available to indicate what character should be used as the decimal separator (default '.').
callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### mjoinMaxCount()

Validate.php : 1129
`public static mjoinMaxCount(mixed $count[, mixed $msg = 'Too many items.' ]) : mixed`

##### Parameters

$count : mixed
$msg : mixed = 'Too many items.'

##### Return values

mixed —

#### mjoinMinCount()

Validate.php : 1116
`public static mjoinMinCount(mixed $count[, mixed $msg = 'Too few items.' ]) : mixed`

##### Parameters

$count : mixed
$msg : mixed = 'Too few items.'

##### Return values

mixed —

#### none()

Validate.php : 237

No validation - all inputs are valid.

`public static none() : callable`

##### Return values

callable —

Validation function

#### noTags()

Validate.php : 879

Check if there are any tags in the submitted value.

`public static noTags([mixed $cfg = null ]) : string|true`

##### Parameters

$cfg : mixed = null

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $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.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### notEmpty()

Validate.php : 345

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, array(
      "empty" => false
    );

##### Parameters

$cfg : ValidateOptions = null

  Validation options

##### Return values

callable —

Validation function

#### numeric()

Validate.php : 409

Check that any input is numeric.

`public static numeric([mixed $decimal = '.' ][, mixed $cfg = null ]) : string|true`

##### Parameters

$decimal : mixed = '.'
$cfg : mixed = null

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  array $opts Validation options. Additional options:

  - `decimal`: is available to indicate what character should be used as the decimal
callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### required()

Validate.php : 315

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, array(
      "required" => true
    );

##### Parameters

$cfg : mixed = null

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  array $opts Validation options. No additional options are available or required for this validation method.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### unique()

Validate.php : 960

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

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $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.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### url()

Validate.php : 777

Validate as an URL address.

`public static url([mixed $cfg = null ]) : string|true`

##### Parameters

$cfg : mixed = null

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  array $opts Validation options. No additional options are available or required for this validation method.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### values()

Validate.php : 845

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

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $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.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

#### xss()

Validate.php : 811

Check if string could contain an XSS attack string.

`public static xss([mixed $cfg = null ]) : string|true`

##### Parameters

$cfg : mixed = null

##### Tags

callback-param

  string $val The value to check for validity

callback-param

  string[] $data The full data set submitted

callback-param

  int|array $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.

callback-param

  array $host Host information

##### Return values

string|true —

true if the value is valid, a string with an error message otherwise.

## Search results