Framework\Validator

Validator assists your application in validating input. The input can be from GET, POST, COOKIE, command line, and even external API calls. These validators can be chained with each other. See examples below.

Methods

  • setData(array $data) : void
  • validate(string $key, bool $recursive = false, string $label = )
  • hasErrors()
  • getError(string $field)
  • getAllErrors(bool $keys = true)
  • getValidData()
  • addError($message)
  • registerError($rule, $key, $message = null)
  • setRule($rule, $function, $message = , $args = [])
  • reset()
  • String Validators
    • email(string $message = null) : Validator
    • required(string $message = null): Validator
    • alphabets(string $message = null): Validator
    • alnum(string $message = null): Validator
    • matches(string $field, string $label, string $message = null): Validator
    • notMatches(string $field, string $label, string $message = null): Validator
    • startsWith(string $string, string $message = null): Validator
    • endsWith(string $string, string $message = null): Validator
    • notStartsWith(string $string, string $message = null): Validator
    • notEndsWith(string $string, string $message = null): Validator
  • Number Validators
    • float(string $message = null): Validator
    • integer(string $message = null): Validator
    • digits(string $message = null): Validator
    • max(int $limit, bool $include = true, string $message = null): Validator
    • between(int $min, int $max, bool $include = true, string $message = null): Validator
  • Length Validators
    • length(int $len, string $message = null): Validator
    • minLength(int $len, string $message = null): Validator
    • maxLength(int $len, string $message = null): Validator
    • betweenLength(int $minlength, int $maxlength, string $message = null): Validator
  • Date Validators
    • date(string $message = null, string $format = null, string $separator = null): Validator
    • formattedDateTime(string $message): Validator
    • formattedDate(string $message): Validator
    • minDate(string $date = 0, string $format = null, string $message = null): Validator
    • maxDate(string $date = 0, string $format = null, string $message = null): Validator
  • Other Validators
    • guid(string $message = null): Validator
    • hex(string $message): Validator
    • colorCode(string $message): Validator
    • ip(string $message = null): Validator
    • url(string $message = null): Validator
    • cnum(string $message = null): Validator
    • oneOf(array $allowed, string $message = null): Validator

Static Methods

You can also use these commonly used validators in a static way, without instantiating the Validator object.

  • isEmail(string $emailID): boolean
  • isString(string $text, bool $onlyAlphabets = false): boolean
  • isNumber(int $number, bool $checkIntFloat = false): boolean
  • isGuid(string $string): boolean
  • isDate(string $date): boolean
  • isDateTime(string $dateTime): boolean
  • isShortGuid(string $string): boolean

Examples

This example shows you the code required to validate data POSTed though a form submit.

// get POST data
$data = \Framework\Request::post(['first_name', 'last_name', 'email_id', 'login_password']);

// instantiate Validator object
$v = new \Framework\Validator();

// set data to validate
$v->setData($data);

// define validation rules
$v->alphabets('Alphabets are required in First name')->required('First name is required')->validate('first_name');
$v->alphabets('Alphabets are required in Last name')->required('Last name is required')->validate('last_name');
$v->email('Email Id is invalid')->required('Email Id is required')->validate('email_id');
$v->required('Password is required')->validate('login_password');

// add acustom error
if (\Helpers\Session::get('flash_error_message') != '') {
    $v->addError(\Helpers\Session::get('flash_error_message'));
}

// check if there are errors
if ($v->hasErrors()) {

    //get all errors
    $errors = $v->getAllErrors();

    // display them
    foreach($errors as $error){
        echo 'Error: '.$error;
    }
}

// clear the error stack
$v->clear();

This example shows the use of static methods of Validator.

// get id from url (GET)
$userId = \Framework\Request::get('id');

// check if it is a number
if(\Framework\Validator::isNumber($userId)){
    // ..
} else {
    // ..
}