You are to note this project is an open source project incase there is an issue and i was unable to respose quickly, try your best to contribute and fix the issue.
run this below comm
$ composer require kayode/form-validator
this library has the following methods and property
use FormValidator\Form;
$validator = new form($_POST);
//validate methods: this is the method were you will be setting your validation rules
$validator->validate([]);
//passed methods: this methods will be use to check if each input passed the validation rules
$validator->passed();
//errors property: this property will be use to output all errors
$validator->errors[];
//errors property is the only available property
the $validator->validate([])
method is use for setting validation rules example:
<?php
use FormValidator\Form;
$validator = new form($_POST);
//check if the request methid is post
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
$validator->validate([
'first_name' => 'required|string|min|3',
'last_name' => 'required|string|min|3',
'github_link' => 'required|url',
]);
}
?>
and the form will look like
<form action='' method='POST'>
<input type='text' name='first_name'><br>
<input type='text' name='last_name'>
<input type='text' name='github_link'>
<button type='submit' name="submit">submit</button>
</form>
to output form errors use the $validator->errors
property
<form action='' method='POST'>
<?php
if($validator->errors['first_name'])
echo '<p>'.$validator->errors['first_name'].'</p>';
?>
<input type='text' name='first_name'><br>
<?php
if($validator->errors['last_name'])
echo '<p>'.$validator->errors['last_name'].'</p>';
?>
<input type='text' name='last_name'>
<?php
if($validator->errors['github_link'])
echo '<p>'.$validator->errors['github_link'].'</p>';
?>
<input type='text' name='github_link'><br>
<button type='submit' name="submit">submit</button>
</form>
looping through errors
<form action='' method='POST'>
<?php
foreach($validator->errors as $error)
{
echo '<p>'.$error.'</p>';
}
?>
<input type='text' name='first_name'><br>
<input type='text' name='last_name'>
<input type='text' name='github_link'><br>
<button type='submit' name="submit">submit</button>
</form>
To use the min
or max
validation rules the next value must be the value for min or max
$validator->validate([
'minimum' => 'min|3',
'maximum' => 'max|6',
]);
To check if each form passed validation rules use the $validator->passed()
method
if($validator->passed()){
echo 'submited sucessfully';
}
These are all the list of available validation rules
required
=> validate empty fieldstring
=> validate a stringemail
=> validate an emailurl
=> validate a urlnumeric
=> validate a mumericint
=> validate an integerfloat
=> validate an floatmin
=> validate minimum value of an input fieldmin
=> validate maximum value of an input field
This is an Open Source proect, you can contribute and add more validation rules