class \mpf\tools\Validator

class \mpf\tools\Validator

Validator can be used to validate input data // Condition structure: key: field name value: array( -> type : field type function to check if is true (function will have as parameters field value, an array with the rest of the conditions and validator object) and must return true or error message(wich can be string or array if there are multiple errors)

           -> min         : used for string, text, list(how many can be selected), date, float and integer
           -> max         : used for string, text, list(how many can be selected), date, float and integer

           -> min_words     : used for string and text
           -> max_words     : used for string and text

           -> min_lines     : used for text
           -> max_lines     : used for text

           -> expression     : used for regexp

           -> values         : used for list and unique(for unique must not be found in this list to be valid)

           -> null             : used for all  - if is not required and is not set then it will became null
           -> required         : used for all
           -> default_value : used for all (if not null and not set)

           -> format         : used for date (php date format)

           -> check_domain  : for email

           -> add_http         : for url(if it doesn't start with: http, https, ftp, ftps)
           -> add_https     : for url(if it doesn't start with: http, https, ftp, ftps)
           -> add_ftp         : for url(if it doesn't start with: http, https, ftp, ftps)
           -> add_ftps         : for url(if it doesn't start with: http, https, ftp, ftps)
           -> online         : for url, will check if is online(via curl)


           -> display_title : the name of the field(in the form that is visible for user)
   )

Example: // first initialize the validator $validator = new Validator(array('rules' => array( array('id', 'int', 'min'=>1), // id must be integer array('firstname, lastname', 'string', 'min'=>3), // firstname and lastname must be strings with minimum 3 characters array('age', 'int', 'min'=>10, 'max'=>105), // age is integer between 10 and 105 array('birthdate', 'date', 'format'=>'d/m/Y H:i', 'min'=>'1960-01-01'), // birthdate must be date, bigger that 1960-01-01, and it will also be formatted after the specified format array('id, firstname, lastname, age, birthdate', 'required', 'on'=>'insert'), // set fields as required on insert array('optionalinfo', 'string', 'null') ))); // optional info can be null. Also if is false or an empty string or array will be transformed in null // validate values $validator->validate(array('id'=>1, 'firstname'=>'Me', 'lastname'=>'Too', 'age'=>23, 'birth_date'=>'01/01/1960')); @since 1.000 @author Mirel Mitache

Attributes
public string[] $rules = []

List of active rules.

public string[] $labels = []

List of fields labels to be used in error messages;

public array $aliases = []

List of condition aliases

protected string[] $errors = []

List of errors for each field transmited;

protected string[] $values

Current values to be validated;

Methods

public string[] getErrors ()

Get list of errors;

public bool validate (string $values, string $action)

  • $values

    -no description found-

  • $action

    -no description found-

Validate current row. Will return true if no errors or false if errors are found. List of errors can be get from $this->getErrors();

public \string[] getValues ()

Return current values as they are sent or updated by previous rules.

public null getValue (mixed $field)

  • $field

    -no description found-

Get value of selected field or null if it doesn't exists

public mixed setValue (string $field, mixed $value)

  • $field

    -no description found-

  • $value

    -no description found-

Change value for a field.

protected bool __checkRule (string[] $rule)

  • $rule

    -no description found-

Validates fields using curent rule

protected bool filterInt (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

Filters integer conditions. Extra options: - strict (can't be real values, just integers) - min (check for min value) - max (check for max value)

protected bool filterString (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

Accepted options: - trim - min (chars) - max (chars) - minwords - maxwords - minlines - maxlines

protected bool filterAlfanumeric (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

Checks if it's string, and then if it's alfanumeric. Same options as for string check.

protected bool filterDigit (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

Checks if it's digit.

protected bool filterInlist (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

Checks if value/values are in list.

protected bool filterRegexp (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

Checks to see if matches the given expression

protected bool filterDate (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

Checks if value is date or time and if it fits in interval

protected bool filterUrl (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

-no description found-

protected bool filterEmail (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

Check if field value is an email address

protected bool filterCompare (string $field, string $details, string $label, string $message)

  • $field

    -no description found-

  • $details

    -no description found-

  • $label

    -no description found-

  • $message

    -no description found-

Compare two fields or a field with a static value.