assegaiphp / validation
Introducing the AssegaiPHP Validation package, the ultimate tool for ensuring the integrity and accuracy of your application's data. With this package, you can easily implement validation rules for incoming data, whether it be from a form submission, API request, or any other source. The package uti
Requires
- php: ^8.2
- arubacao/tld-checker: ^1.2
- giggsey/libphonenumber-for-php: ^8.13
Requires (Dev)
This package is auto-updated.
Last update: 2024-10-30 01:59:45 UTC
README
A progressive PHP framework for building efficient and scalable server-side applications.
AssegaiPHP Validation
Welcome to the AssegaiPHP validation package! This package is designed to provide a simple and easy-to-use solution for validating data in your AssegaiPHP projects.
Installation
To install this package, run the following command in your project's root directory:
composer require assegaiphp/validation
Basic Usage
To use this package, simply import the Assegai\Validation\Validator
class and create a new instance. You can then use the validate()
method to check if a value or array of values meet the specified criteria.
use Assegai\Validation\Validator; $validator = new Validator(); $isValid = $validator->validate($value, 'required|email');
The second argument to the validate()
method is a string containing the validation rules to apply, separated by the |
character. In the example above, the required and email rules are applied.
Create a Validation Rule
use Assegai\Validation\Interfaces\IValidationRule; class MyCustomValidationRule extends IValidationRule { public function passes(mixed $value): bool { // validate the value } public function getErrorMessage(): string { return 'The value is invalid.'; } }
Available Rules
This package comes with a number of built-in validation rules, including:
alpha
: value must contain only alphabetic charactersalnum
: value must contain only alphanumeric charactersbetween:n,m
: value must be between n and m (inclusive)domain
: value must be a valid domain nameemail
: value must be a valid email addressequalTo:n
: value must be equal to nfloat
: value must be a floating point numberinlist:l
: value is in a specific list of allowed valuesinteger
: value must be an integermaxLength:n
: value must be no more than n characters longmax:n
: value does not exceed a maximum value, nminLength:n
: value must be at least n characters longmin:n
: value is at least a certain value, nnotEqualTo:n
: value must not be equal to nnotinlist:l
: value is not in a specific list of disallowed valuesnumeric
: value must be a numberregex:n
: value must match the given regex pattern nrequired
: value must be presentstring
: value must be a stringurl
: value must be a valid url
You can also create custom validation rules by extending the Assegai\Validation\Rule class and implementing the validate() method.
Validation Attributes
Error Handling
If the validate() method returns false, you can use the getErrors() method to retrieve an array of error messages.
if (!$isValid) { $errors = $validator->getErrors(); // handle errors }
Further Reading
For more information on how to use this package, please see the full documentation. Thank you for using the AssegaiPHP.