# Class 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 `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:

    // 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;
         } )

Located at Editor/Validate.php

Methods summary
`public static callable` | # `none( )` No validation - all inputs are valid. No validation - all inputs are valid. #### Returns callable Validation function
`public static string|true` | # `basic( string $cfg = null , array $opts,…, array $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 $cfg $val The value to check for validity $opts,… Validation options. No additional options are available or required for this validation method. $host,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `required( string $cfg = null , array $opts,…, array $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 $cfg $val The value to check for validity $opts,… Validation options. No additional options are available or required for this validation method. $host,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static callable` | # `notEmpty( `DataTables\Editor\ValidateOptions` $cfg = null )` 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 $cfg Validation options #### Returns callable Validation function
`public static string|true` | # `boolean( string $cfg = null , array $opts,…, array $host,… )` Validate an input as a boolean value. Validate an input as a boolean value. #### Parameters $cfg $val The value to check for validity $opts,… Validation options. No additional options are available or required for this validation method. $host,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `numeric( string $decimal = ".", string[] $cfg = null , array $opts,…, array $host,… )` Check that any input is numeric. Check that any input is numeric. #### Parameters $decimal $val The value to check for validity $cfg $data The full data set submitted $opts,… Validation options. Additional options: * `decimal`: is available to indicate what character should be used as the decimal $host,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `minNum( string $min, string[] $decimal = ".", integer|array $cfg = null , array $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 $min $val The value to check for validity $decimal $data The full data set submitted $cfg $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 '.'). $host,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `maxNum( string $max, string[] $decimal = ".", integer|array $cfg = null , array $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 $max $val The value to check for validity $decimal $data The full data set submitted $cfg $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 $host,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `minMaxNum( string $min, string[] $max, integer|array $decimal = '.', array $cfg = null )` 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 $min $val The value to check for validity $max $data The full data set submitted $decimal $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 $cfg $host Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `email( string $cfg = null , array $opts,…, array $host,… )` Validate an input as an e-mail address. Validate an input as an e-mail address. #### Parameters $cfg $val The value to check for validity $opts,… Validation options. No additional options are available or required for this validation method. $host,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `minLen( string $min, string[] $cfg = null , integer|array $opts,…, array $host,… )` Validate a string has a minimum length. Validate a string has a minimum length. #### Parameters $min $val The value to check for validity $cfg $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,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `maxLen( string $max, string[] $cfg = null , integer|array $opts,…, array $host,… )` Validate a string does not exceed a maximum length. Validate a string does not exceed a maximum length. #### Parameters $max $val The value to check for validity $cfg $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,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `minMaxLen( string $min, string[] $max, integer|array $cfg = null , array $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 $min $val The value to check for validity $max $data The full data set submitted $cfg $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,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `ip( string $cfg = null , array $opts,…, array $host,… )` Validate as an IP address. Validate as an IP address. #### Parameters $cfg $val The value to check for validity $opts,… Validation options. No additional options are available or required for this validation method. $host,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `url( string $cfg = null , array $opts,…, array $host,… )` Validate as an URL address. Validate as an URL address. #### Parameters $cfg $val The value to check for validity $opts,… Validation options. No additional options are available or required for this validation method. $host,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `xss( string $cfg = null , integer|array $opts,…, array $host,… )` Check if string could contain an XSS attack string Check if string could contain an XSS attack string #### Parameters $cfg $val The value to check for validity $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,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `values( string $values, string[] $cfg = null , integer|array $opts,…, array $host,… )` Confirm that the value submitted is in a list of allowable values Confirm that the value submitted is in a list of allowable values #### Parameters $values $val The value to check for validity $cfg $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,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `noTags( string $cfg = null , integer|array $opts,…, array $host,… )` Check if there are any tags in the submitted value Check if there are any tags in the submitted value #### Parameters $cfg $val The value to check for validity $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,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `dateFormat( string $format, string[] $cfg = null , array|string $opts,…, array $host,… )` Check that a valid date input is given Check that a valid date input is given #### Parameters $format $val The value to check for validity $cfg $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,… Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `unique( string $cfg = null, string[] $column = null, integer|array $table = null, array $db = null )` Check that the given value is unique in the database Check that the given value is unique in the database #### Parameters $cfg $val The value to check for validity $column $data The full data set submitted $table $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. $db $host Host information #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static string|true` | # `dbValues( string $cfg = null, string[] $column = null, integer|array $table = null, array $db = null, $values = array() )` 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. 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. #### Parameters $cfg $val The value to check for validity $column $data The full data set submitted $table $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. $db $host Host information $values #### Returns string|true true if the value is valid, a string with an error message otherwise.
`public static` | # `fileExtensions( $extensions, $msg = "This file type cannot be uploaded." )`
`public static` | # `fileSize( $size, $msg = "Uploaded file is too large." )`
`public static` | # `mjoinMinCount( $count, $msg = "Too few items." )`
`public static` | # `mjoinMaxCount( $count, $msg = "Too many items." )`