To install envalid just run the following command in your projects root directory
composer require azi/envalid
Or add the following in your composer.json
file
"require":{
"azi/envalid":"^1.0"
}
To install envalid just run the following command in your projects root directory
composer require azi/envalid
Or add the following in your composer.json
file
"require":{
"azi/envalid":"^1.0"
}
...
'description' => 'length:>=:50' # in this example `>=` and `50` are our variables
...
$validator = new azi\Envalid();
$validator->validate($_POST, [
'username' : 'required--message=Please provide a username',
'password' : 'password:medium--key=value&key2=value2'
]);
Below is a list of all available validation rules
...
'new_password' => 'password:strong'
...
...
'age' => 'min:18'
...
...
'age' => 'max:35'
...
length:{operator}:{value}
It alose can be like this length:{value}
...
'description' => 'length:=>:50', // string length must be greater than or equal to 50
'bio' => 'length:<=:250', // string length must be less than or equal to 250
'code' => 'length:6' // string length must be equal to 6
...
array:{csv_string}
...
'hobbies' => 'array', // the field must be an array
'languages' => 'array:php,javascript', // it must be an array and contains php and javascript
...
...
'play_games' => 'boolean', // the field must be boolean
'hate_animals' => 'boolean:false', // it must be false
...
...
'address' => 'ip'
...
...
'confirm_password' => 'same:password', // the value of `confirm_password` must be same as `password`
...
...
'intro_video' => 'file:video',
'profile_picture' => 'file:image',
'cover_letter' => 'file:doc',
...
$validator = new azi\Envalid();
$validator->validate($_POST, [
'username' => 'required',
'email' => 'required|email',
'password' => 'required|password:strong',
'confirm_password' => 'same:password'
]);
if($validator->failed()) {
return [
'status' => 'validation-failed',
'message' => 'Please fill all the required fields correctly',
'errors' => $validator->getErrors()
];
}
$validator = new azi\Envalid();
// to validate files we will need to provide the $_FILES data to Envalid
$data = array_merge($_POST, $_FILES);
$validator->validate($data, [
'bucket' => 'required',
'image' => 'file:image',
]);
if($validator->failed()) {
return [
'status' => 'validation-failed',
'message' => 'Make sure you provided all the information correctly',
'errors' => $validator->getErrors()
];
}
addRule
method on Envalid
class
$validator = new azi\Envalid();
$validator->addRule('rule_name', 'Callable|Closure|azi\Rules\Contracts\RuleInterface');
$validator = new azi\Envalid();
$validator->addRule('recaptcha', function($field, $value, azi\Arguments $args){
if(/* send a request to google to validate the recaptcha*/) {
return true;
}
return "You failed to prove that your're a human :) ";
});
# lets validate our password reset form
$validator->validate($_POST, [
'email' => 'required|email',
'captcha' => 'recaptcha' // We're using our custom rule
])
<?php
use azi\Arguments;
use azi\Rules\Contracts\RuleInterface;
class RecaptchaRule implements RuleInterface
{
/**
* @param $field
* @param $value
* @param Arguments $args
* @return boolean
*/
public function validate( $field, $value, Arguments $args )
{
if(/* send a request to google to validate the recaptcha*/) {
return true;
}
return false;
}
/**
* The message if the validation fails
*
* @return string
*/
public function message()
{
/* we can use {field} placeholder in the following string
it will be replaced with the field label
*/
return "You failed to prove that your're a human :) ";
}
}
$validator = new azi\Envalid();
# This looks more clean isn't it ?
$validator->addRule('recaptcha', new RecaptchaRule());
# lets validate our password reset form
$validator->validate($_POST, [
'email' => 'required|email',
'captcha' => 'recaptcha' // We're using our custom rule
])