-
Notifications
You must be signed in to change notification settings - Fork 2
FormValidation Class Documentation
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
numericvalidation rule to the current field. -
integer($custom_message=null): Adds the
integervalidation rule to the current field. -
string($custom_message=null): Adds the
stringvalidation rule to the current field. -
digits(int $digits,$custom_message=null): Adds the
digitsvalidation rule to the current field. -
digitsBetween(int $min,int $max,$custom_message=false): Adds the
digitsBetweenvalidation rule to the current field. -
different($other_value,$custom_message=false): Adds the
differentvalidation rule to the current field. -
confirmed($field,$custom_message=false): Adds the
confirmedvalidation rule to the current field. -
min($min_value,$custom_message=null): Adds the
minvalidation rule to the current field. -
max($max_value,$custom_message=null): Adds the
maxvalidation rule to the current field. -
required($custom_message=false): Adds the
requiredvalidation rule to the current field. -
email($custom_message=false): Adds the
emailvalidation rule to the current field. -
url($custom_message=false): Adds the
urlvalidation rule to the current field. -
domain($custom_message=false): Adds the
domainvalidation rule to the current field. -
mac($custom_message=false): Adds the
macvalidation rule to the current field. -
ip($custom_message=false): Adds the
ipvalidation rule to the current field. -
boolean($custom_message=false): Adds the
booleanvalidation rule to the current field. -
array($custom_message=false): Adds the
arrayvalidation rule to the current field. -
object($custom_message=false): Adds the
objectvalidation rule to the current field. -
between($min,$max,$custom_message=null): Adds the
betweenvalidation 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.