fyre / validation
A validation library.
Requires
- fyre/container: ^1.0
- fyre/lang: ^4.0
- fyre/typeparser: ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^11
README
FyreValidation is a free, open-source validation library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/validation
In PHP:
use Fyre\Validation\Validator;
Basic Usage
$container
is a Container.$typeParser
is a TypeParser.$lang
is a Lang.
$validator = new Validator($container, $typeParser, $lang);
Autoloading
Any dependencies will be injected automatically when loading from the Container.
$validator = $container->use(Validator::class);
Methods
Add
Add a validation rule.
$field
is a string representing the field name.$rule
is a Closure or a Rule representing the validation rule.$options
is an array containing options for the validation rule.on
is a string representing the type of validation the rule applies to, and will default to null.message
is a string representing the error message for the rule, and will default to null.name
is a string representing the name of the validation rule, and will default to null.
$validator->add($field, $rule, $options);
If using a Closure rule, the $rule
should be expressing in the following format:
$rule = function(mixed $value, array $data, string $field): string|bool { // validation logic return true; };
Clear
Clear all rules from the Validator.
$validator->clear();
Get Field Rules
Get the rules for a field.
$field
is a string representing the field name.
$rules = $validator->getFieldRules($field);
Remove
Remove a validation rule.
$field
is a string representing the field name.$name
is a string representing the rule name.
$removed = $validator->remove($field, $name);
If the $name
argument is omitted, all rules will be removed instead.
$validator->remove($field);
Validate
Perform validation and return any errors.
$data
is an array containing the data to validate.$type
is a string representing the type of validation, and will default to null.
$errors = $validator->validate($data, $type);
Rules
use Fyre\Validation\Rule;
Alpha
Create an "alpha" Rule.
Rule::alpha();
Alpha Numeric
Create an "alpha-numeric" Rule.
Rule::alphaNumeric();
Ascii
Create an "ASCII" Rule.
Rule::ascii();
Between
Create a "between" Rule.
$min
is a number representing the minimum value (inclusive).$max
is a number representing the maximum value (inclusive).
Rule::between($min, $max);
Boolean
Create a "boolean" Rule.
Rule::boolean();
Decimal
Create a "decimal" Rule.
Rule::decimal();
Date
Create a "date" Rule.
Rule::date();
DateTime
Create a "date/time" Rule.
Rule::dateTime();
Differs
Create a "differs" Rule.
$field
is a string representing the other field to compare against.
Rule::differs($field);
Create an "email" Rule.
Rule::email();
Empty
Create an "empty" Rule.
Rule::empty();
Equals
Create an "equals" Rule.
$value
is the value to compare against.
Rule::equals($value);
Exact Length
Create an "exact length" Rule.
$length
is a number representing the length.
Rule::exactLength($length);
Greater Than
Create a "greater than" Rule.
$min
is the minimum value.
Rule::greaterThan($min);
Greater Than Or Equals
Create a "greater than or equals" Rule.
$min
is the minimum value.
Rule::greaterThanOrEquals($min);
In
Create an "in" Rule.
$values
is an array containing the values to compare against.
Rule::in($values);
Integer
Create an "integer" Rule.
Rule::integer();
Ip
Create an "IP" Rule.
Rule::ip();
Ipv4
Create an "IPv4" Rule.
Rule::ipv4();
Ipv6
Create an "IPv6" Rule.
Rule::ipv6();
Less Than
Create a "less than" Rule.
$max
is the maximum value.
Rule::lessThan($max);
Less Than Or Equals
Create a "less than or equals" Rule.
$max
is the maximum value.
Rule::lessThanOrEquals($max);
Matches
Create a "matches" Rule.
$field
is a string representing the other field to compare against.
Rule::matches($field);
Max Length
Create a "maximum length" Rule.
$length
is a number representing the maximum length.
Rule::maxLength($length);
Min Length
Create a "minimum length" Rule.
$length
is a number representing the minimum length.
Rule::minLength($length);
Natural Number
Create a "natural number" Rule.
Rule::naturalNumber();
Not Empty
Create an "not empty" Rule.
Rule::notEmpty();
Regex
Create a "regular expression" Rule.
$regex
is a string representing the regular expression.
Rule::regex($regex);
Required
Create a "required" Rule.
Rule::required();
Require Presence
Create a "require presence" Rule.
Rule::requirePresence();
Time
Create a "time" Rule.
Rule::time();
Url
Create a "URL" Rule.
Rule::url();
Error Messages
Custom error messages can be used by supplying the message
property of the $options
array to the Validator add
method.
$validator->add('field', Rule::required(), [ 'message' => 'The field is required.' ]);
Alternatively, for custom validation callbacks, a string can be returned and that will be used as the error messages.
$validator->add('field', function(mixed $value): bool|string { if ($value) { return true; } return 'The field is required.'; });
If a custom error message is not supplied, the rule name will be used to retrieve a Lang value. The field placeholder can be used for the field name, and any arguments supplied to the rule will be available as numeric placeholders.
// language/en/Validation.php return [ 'required' => 'The {field} is required.' ];
If no error message is available, the error message will simply be set to "invalid".