A simple PHP package for data validation with extensive preset rules and custom rules.
You can start it from composer. Go to your terminal and run this command from your project root directory.
composer require hashemi/valideto
After Complete installation, It's time to check how to use Valideto easily.
<?php
use Hashemi\Valideto\Valideto;
$data = [
'first_name' => "Hashemi",
'last_name' => "Rafsan" ,
'email' => 'rafsan@xyz.com'
];
$validator = new Valideto($data, [
'first_name' => ['required', 'string'],
'last_name' => ['required', 'string'],
'email' => ['required', 'email']
]);
// Call "validate" for validating your data
$validator->validate();
if ($validator->success()) {
// do something...
}
if ($validator->fails()) {
// do something if fails
}
You should be use that when you want to validate your data. Valideto expose many default rules for validation but if user need to make by own they can also do that. Already there is option to change the default rules logic if you don't like to use. Valideto provide an interface for change default validation rules logic.
Then let's check how you can do that but recommended to not change it, Once if you change it's you will be liable to result
<?php
use Hashemi\Valideto\Rules\DefaultRulesInterface;
use Hashemi\Valideto\Valideto;
class OwnRulesClass implements DefaultRulesInterface
{
public function setData(array $data): self {}
public function isRequired(string $key): bool {}
public function isNullable(string $key): bool {}
public function isArray(string $key, bool $nullable = false): bool {}
public function isAssoc(string $key, bool $nullable = false): bool {}
public function isString(string $key, bool $nullable = false): bool {}
public function isNumeric(string $key, bool $nullable = false): bool {}
public function isDistinct(string $key, bool $nullable = false): bool {}
public function isInteger(string $key, bool $nullable = false): bool {}
public function isFloat(string $key, bool $nullable = false): bool {}
public function isBoolean(string $key, bool $nullable = false): bool {}
public function isSize(string $key, int $length, bool $nullable = false): bool {}
public function isMax(string $key, int $value, bool $nullable = false): bool {}
public function isMin(string $key, int $value, bool $nullable = false): bool {}
}
$data = [
'first_name' => "Hashemi",
'last_name' => "Rafsan" ,
'email' => 'rafsan@xyz.com'
];
$validator = new Valideto($data, [
'first_name' => ['required', 'string'],
'last_name' => ['required', 'string'],
'email' => ['required', 'email']
]);
// Call "validate" for validating your data
$validator->setRulesClass(new OwnRulesClass());
$validator->validate();
Do it, your own risk :D
- required
- max
- min
- gt
- gte
- lt
- lte
- eq
- nullable
- distinct
- date
- date_format
- array
- url
- ip
- boolean
- string
- digits
- size
- numeric
- integer
- float
- assoc
required should be use for when you expect that value in your data
Example:
$validator = new Valideto($data, [
'first_name' => ['required'],
]);
max should be use for when you need to check if value exceed max value or not
Example:
$validator = new Valideto($data, [
'age' => ['max:24'],
]);
min should be use for when you need to check if value have at least minimum value or not
Example:
$validator = new Valideto($data, [
'age' => ['min:24'],
]);
gt should be use for when you need to check if value greater than or not
Example:
$validator = new Valideto($data, [
'age' => ['gt:24'],
]);
gte should be use for when you need to check if value greater than or equal
Example:
$validator = new Valideto($data, [
'age' => ['gte:24'],
]);
lt should be use for when you need to check if value less than or not
Example:
$validator = new Valideto($data, [
'age' => ['lt:24'],
]);
lte should be use for when you need to check if value less than or equal
Example:
$validator = new Valideto($data, [
'age' => ['lte:24'],
]);
eq should be use for when you need to check if value equal
Example:
$validator = new Valideto($data, [
'age' => ['eq:integer|float|string|boolean:24'],
]);
nullable should be use for when value is not required
Example:
$validator = new Valideto($data, [
'age' => ['nullable'],
]);
distinct should be use for when you don't duplicate value in array
Example:
$validator = new Valideto($data, [
'hobbies' => ['array', 'distinct'],
]);
date should be use for when you check date is valid or not
Example:
$validator = new Valideto($data, [
'start_date' => ['date'],
]);
date_format should be use for when you check date format is valid or not
Example:
$validator = new Valideto($data, [
'start_date' => ['date_format:Y-m-d'],
]);
array should be use for when you check the data is array or not
Example:
$validator = new Valideto($data, [
'start_date' => ['date_format:Y-m-d'],
]);
url should be use for when you check the data is url or not
Example:
$validator = new Valideto($data, [
'website' => ['url'],
]);
ip should be use for when you check the data is ip or not
Example:
$validator = new Valideto($data, [
'ip' => ['ip'],
]);
boolean should be use for when you check the data is boolean or not
Example:
$validator = new Valideto($data, [
'is_enable' => ['boolean'],
]);
email should be use for when you check the data is email or not
Example:
$validator = new Valideto($data, [
'email' => ['email'],
]);
string should be use for when you check the data is string or not
Example:
$validator = new Valideto($data, [
'first_name' => ['string'],
]);
numeric should be use for when you check the data is numeric or not
Example:
$validator = new Valideto($data, [
'id' => ['numeric'],
]);
integer should be use for when you check the data is integer or not
Example:
$validator = new Valideto($data, [
'id' => ['integer'],
]);
float should be use for when you check the data is float or not
Example:
$validator = new Valideto($data, [
'price' => ['float'],
]);
assoc should be use for when you check the data is associative array or not
Example:
$validator = new Valideto($data, [
'hobbies' => ['array', 'assoc'],
]);
Pull requests are welcome. For any changes, please open an issue first to discuss what you would like to change.