Download .zip |
Info | Documentation | View files (31) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2020-06-20 (Yesterday) | Not yet rated by the users | Total: 1 This week: 1 | All time: 9,984 This week: 308 |
Version | License | PHP version | Categories | |||
php-flexible-validat 1.0 | The PHP License | 5 | PHP 5, Validation |
Description | Author | |
This package can validate values according to flexible rules. |
Provides several different approaches to validate your application's incoming data
You can install the package via composer:
composer require infinitypaul/php-validator
<?php
$validator = new Validator([
'full_name' => 'Edward Paul',
'email' => 'infinitypaul@live.com'
]);
$validator->setRules([
'full_name' => ['required'],
'email' => ['email']
]);
if(!$validator->validate()){
var_dump($validator->getErrors());
}else {
var_dump('Passed');
}
>
We pass the field we intend to validate into the Validator
class
new Validator([
'full_name' => 'Edward Paul',
'email' => 'infinitypaul@live.com'
]);
we pass the desired validation rules into the setRules()
method. Again, if the validation fails, the proper response will save in the ErrorBag. If the validation passes, our validate method returns a true statement.
<?php
$validator->setRules([
'full_name' => ['required'],
'email' => ['required','email']
]);
So, what if the incoming parameters do not pass the given validation rules? The errors will be able in the getErrors()
method like the above
$validator->getErrors();
You may also use the validate()
method to quickly check if validation error messages exist. It returns boolean:
if(!$validator->validate()){
//There is an error
var_dump($validator->getErrors());
}else {
//No Error
var_dump('Passed');
}
If you can enter full_name as the key to be validated and required is set, your error message comes out in this format full_name is required
You can customize the full_name
with the setAliases
method.
<?php
$validator->setAliases([
'full_name' => 'Full Name',
'email' => 'Email Address'
]);
Validating array doesn't have to be a pain. You may use "dot notation" to validate attributes within an array. For example, if the incoming request contains an array field, you may validate it like so:
$validator = new Validator([
'email' => [
'infinitypaul@live.com',
''
]
]);
$validator->setRules([
'email' => [
'required',
'email'
],
]);
You may also validate each element of an array. For example, to validate that each e-mail in a given array input field is unique, you may do the following:
$validator = new Validator([
'user' => [
[
'firstName' => ''
],
[
'firstName' => 'Paul'
]
],
]);
$validator->setRules([
'user.*.firstName' => [
'required'
]
]);
You may wish to specify some of your own rules. One method of registering custom validation rules is extending the Rule Class
Once the custom rule class has been created, we are ready to define its behavior. A rule object contains two methods: passes and message. The passes method receives the field, value, and data, and should return true or false depending on whether the value is valid or not. The message method should return the validation error message that should be used when validation fails:
use Infinitypaul\Validator\Rules\Rule;
class Uppercase extends Rule
{
//Determine if the validation rule passes
public function passes($field, $value, $data): bool
{
return strtoupper($value) === $value;
}
//Get the validation error message.
public function message($field): string
{
return $field.' Must Be Uppercase';
}
}
Once the rule has been defined, you may attach it to a validator by passing an instance of the rule object with your other validation rules:
$validator = new Validator([
'name' => 'Edward Paul',
]);
$validator->setRules([
'name' => [
new Uppercase(),
'max:5'
]
]);
Below is a list of all available validation rules and their function
The field under validation must be present in the input data and not empty.
The field under validation must be formatted as an e-mail address
The field under validation must be less than or equal to a maximum value
max:20
The field under validation must be present and not empty only if all of the other specified fields are present.
required_with:lastName,middle
The field under validation must have a size between the given min and max
between:10,20
The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values.
optional
The file under validation must be an image (jpeg, png, bmp, gif, svg, or webp), the image tmp_name
is what you need to pass to the validator
$validator = new Validator([
'picture' => $_FILES['image']['tmp_name'],
]);
$validator->setRules([
'picture' => 'image'
]);
The given field must match the field under validation.
same:captha
The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
The field under validation must be numeric.
I intend to keep adding more rules to the package but If you have any additional rules you will like me to add to this, you can reach out to me or open an issue in that regard.
Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word!
Don't forget to follow me on twitter || or on medium
Thanks! Edward Paul.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you have spotted any bugs, or would like to request additional features from the library, please file an issue via the Issue Tracker on the project's Github page: https://github.com/infinitypaul/php-validator/issues.
The MIT License (MIT). Please see License File for more information.
Files |
File | Role | Description | ||
---|---|---|---|---|
.idea (5 files, 1 directory) | ||||
src (2 files, 2 directories) | ||||
tests (1 file) | ||||
.editorconfig | Data | Auxiliary data | ||
.scrutinizer.yml | Data | Auxiliary data | ||
.styleci.yml | Data | Auxiliary data | ||
.travis.yml | Data | Auxiliary data | ||
CHANGELOG.md | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
CONTRIBUTING.md | Data | Auxiliary data | ||
LICENSE.md | Lic. | License text | ||
phpunit.xml.dist | Data | Auxiliary data | ||
README.md | Doc. | Read me |
Files | / | .idea |
File | Role | Description | ||
---|---|---|---|---|
dictionaries (1 file) | ||||
misc.xml | Data | Auxiliary data | ||
modules.xml | Data | Auxiliary data | ||
php-validator.iml | Data | Auxiliary data | ||
php.xml | Data | Auxiliary data | ||
vcs.xml | Data | Auxiliary data |
Files | / | src |
File | Role | Description | ||
---|---|---|---|---|
Errors (1 file) | ||||
Rules (11 files) | ||||
RuleMap.php | Class | Class source | ||
Validator.php | Class | Class source |
Files | / | src | / | Rules |
File | Role | Description |
---|---|---|
Between.php | Class | Class source |
Confirmed.php | Class | Class source |
Email.php | Class | Class source |
Image.php | Class | Class source |
Max.php | Class | Class source |
Numeric.php | Class | Class source |
Optional.php | Class | Class source |
Required.php | Class | Class source |
RequiredWith.php | Class | Class source |
Rule.php | Class | Class source |
Same.php | Class | Class source |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.