Documentation

Validator

The Validator class provides methods for validating input data against specified rules, ensuring data integrity and correctness. It supports various validation rules such as required fields, string validation, numeric validation, email validation, and more.

Usage Example

if (!Validator::username($username)) throw new \Exception("Invalid username format");
if (!Validator::email($email)) throw new \Exception("Invalid email format");
if (!Validator::password($password)) throw new \Exception("Invalid password format");

Methods

  • validate(array $input, array $rules)
    Validates an associative array of values.
    • $input: An associative array where keys correspond to input value names. Example: ['name' => 'Foo', 'age' => 42, 'email' => 'foo@bar.com']
    • $rules: An associative array where keys correspond to field names to validate, and values correspond to validation rules separated by '|'. Example: ['name' => 'string', 'age' => 'numeric', 'email' => 'email']
    • This method returns array|bool: Returns true on success, otherwise returns an array containing the fields that did not pass validation.
  • validateFormValues(array $input, array $rules)
    Validates form values received from the frontend.
    • $input: An associative array where keys correspond to field IDs and values contain an array with the input values. Example: [12 => ["values" => ["Alessio"]], 3 => ["values" => ["35"]]]
    • $rules: An associative array where keys correspond to field IDs and values contain validation rules separated by '|'. Example: [12 => "Required|String", 3 => "Required|Integer"]
    • This method returns array|bool: Returns true on success, otherwise returns an array containing the fields that did not pass validation.
  • string($input)
    Validate if the input is a string or an array of strings.
    • $input: The input value to validate.
    • This method returns bool: True if the input is a string or array of strings, false otherwise.
  • onlyChars($input)
    Validate strings containing only letters.
    • $input: The input string to validate.
    • This method returns int: 1 if the string only contains letters, 0 otherwise.
  • onlyCharsWithSpace($input)
    Validate strings containing only letters and spaces.
    • $input: The input string to validate.
    • This method returns int: 1 if the string only contains letters and spaces, 0 otherwise.
  • onlyCharNumbersUnderscore($input)
    Validate strings containing only letters, numbers, or underscores.
    • $input: The input string to validate.
    • This method returns int: 1 if the string only contains letters, numbers, and underscores, 0 otherwise.
  • address($input)
    Validate addresses like Google Maps default address or simply letters, numbers, accents, "-", "_", ",", and spaces.
    • $input: The input string to validate.
    • This method returns int: 1 if the address is valid, 0 otherwise.
  • operators($rule)
    Validate operators for query building.
    • $rule: The rule string to validate.
    • This method returns bool: True if the operator is valid, false otherwise.
  • numeric($input)
    Validate if the input is numeric or an array of numerics.
    • $input: The input value to validate.
    • This method returns bool: True if the input is numeric or an array of numerics, false otherwise.
  • alphanumeric($input)
    Validate if the input is alphanumeric or an array of alphanumeric strings.
    • $input: The input value to validate.
    • This method returns bool|int: True or 1 if the input is alphanumeric or an array of alphanumeric strings, false or 0 otherwise.
  • integer($input)
    Validate if the input is an integer or an array of integers.
    • $input: The input value to validate.
    • This method returns bool|int: True or 1 if the input is an integer or an array of integers, false or 0 otherwise.
  • float($input)
    Validate if the input is a float number or an array of float numbers.
    • $input: The input value to validate.
    • This method returns bool|int: True or 1 if the input is a float or an array of float numbers, false or 0 otherwise.
  • in($elem, $array)
    Check if an element exists in an array.
    • $elem: The element to check.
    • $array: The array to search.
    • This method returns bool: True if the element exists in the array, false otherwise.
  • email($input)
    Validate if the input is a valid email address.
    • $input: The input value to validate.
    • This method returns bool: True if the input is a valid email address, false otherwise.
  • phone($input)
    Validate if the input is a phone number.
    • $input: The input value to validate.
    • This method returns bool: True if the input matches the phone number pattern, false otherwise.
  • password_login($password)
    Validate password for login purposes.
    • $password: The password string to validate.
    • This method returns bool: True if the password is valid, false otherwise.
  • password($password)
    Validate a password.
    • $password: The password string to validate.
    • This method returns bool: True if the password is valid, false otherwise.
  • strongPassword($password)
    Validate if the password is strong.
    • $password: The password string to validate.
    • This method returns int: 1 if the password is strong, 0 otherwise.
  • username($username)
    Validate a username.
    • $username: The username string to validate.
    • This method returns bool: True if the username is valid, false otherwise.
  • filename($filename)
    Validate a filename.
    • $filename: The filename string to validate.
    • This method returns bool: True if the filename is valid. Note: This method is currently not fully implemented.
  • required($input, $array)
    Check if a key exists in an array.
    • $input: The key to check.
    • $array: The array to search.
    • This method returns bool: True if the key exists in the array, false otherwise.
  • setError(string $key, string $message)
    Set an error message.
    • $key: The key associated with the error.
    • $message: The error message to set.
    • This method returns void.
  • hasError()
    Check if there's an error.
    • This method returns bool: True if there is an error, false otherwise.