Skip to content

FormValidation Class Documentation

Benyamin Khalife edited this page Nov 23, 2023 · 5 revisions

The FormValidation class provides features that can validate the information received from the user with the desired features.

$form = new FormValidation();

$form
  ->field('name')->required()->min(3)
  ->field('email')->required()->email()
  ->field('age')->min(18)
  ->field('password')->required()->min(6)->confirmed('confirm_password');

if($form->isValid() == false){
  echo $form->getFirstErrorMessage();
}
else{
 // ....
}

The error text is received from the validation.php file located in the app/Storage/Langs/en/ path.

You can customize that file according to your needs or your language. Also, you can easily specify error messages when writing the validation code, as in the example below:

$input = [
  'email'=> 'invalidemailstring',
];

$form = new FormValidation($input );

$form
  ->field('email')
     ->required('Please enter the email field')
     ->email('The email is not valid :(')

if($form->isValid() == false){
  echo $form->getFirstErrorMessage();
}
else{
 // ....
}

Output: The email is not valid :(

Methods:

  • field(string $name,$translation=null): Sets the current field to validate.

  • numeric($custom_message=null): Adds the numeric validation rule to the current field.

  • integer($custom_message=null): Adds the integer validation rule to the current field.

  • string($custom_message=null): Adds the string validation rule to the current field.

  • digits(int $digits,$custom_message=null): Adds the digits validation rule to the current field.

  • digitsBetween(int $min,int $max,$custom_message=false): Adds the digitsBetween validation rule to the current field.

  • different($other_value,$custom_message=false): Adds the different validation rule to the current field.

  • confirmed($field,$custom_message=false): Adds the confirmed validation rule to the current field.

  • min($min_value,$custom_message=null): Adds the min validation rule to the current field.

  • max($max_value,$custom_message=null): Adds the max validation rule to the current field.

  • required($custom_message=false): Adds the required validation rule to the current field.

  • email($custom_message=false): Adds the email validation rule to the current field.

  • url($custom_message=false): Adds the url validation rule to the current field.

  • domain($custom_message=false): Adds the domain validation rule to the current field.

  • mac($custom_message=false): Adds the mac validation rule to the current field.

  • ip($custom_message=false): Adds the ip validation rule to the current field.

  • boolean($custom_message=false): Adds the boolean validation rule to the current field.

  • array($custom_message=false): Adds the array validation rule to the current field.

  • object($custom_message=false): Adds the object validation rule to the current field.

  • between($min,$max,$custom_message=null): Adds the between validation rule to the current field.

  • addError(string $field_name,array $validation_result,array $rule): Adds an error message to the error list.

  • getFirstError(): Returns the first error in the error list.

  • getFirstErrorMessage(): Returns the message of the first error in the error list.

  • getAllError(): Returns the entire error list.

  • isValid(): Returns the validation result. If the information was sent correctly and the validation was successful, it will return true value, otherwise it will return false value.

Clone this wiki locally