Alexya's Validator utilities
Alexya's validator components, (*1)
The class \Alexya\Validator\Validator
provides helpers for validating input., (*3)
You'll need to instance a Validator
object, add the fields to validate and call the validate
method., (*4)
The constructor can accept as parameter an array of \Alexya\Validator\Ruler
objects.
You can also add a new ruler using the method addRuler
., (*6)
Example:, (*7)
<?php $validator = new \Alexya\Validator\Validator([ new \Alexya\Validator\Rulers\StringRuler(), new \Alexya\Validator\Rulers\IntegerRuler() ]); $validator->addRuler(new \Alexya\Validator\Rulers\GenericRuler());
For adding a field use the method add
which accepts as parameter the filed name and its value and returns
an object of type \Alexya\Validator\Field
which is the field., (*9)
Once you've added all the fields use the method validate
to validate the fields.
If a string is sent as parameter, it will validate the field with that name, if not it will
validate all fields.
It returns true
if all fields have been validated successfully, false
if not., (*11)
The method getErrors
returns an array containing validation errors., (*12)
Example:, (*13)
<?php $validator = new \Alexya\Validator\Validator([); $validator->addRuler(new \Alexya\Validator\Rulers\GenericRuler()); $validator->addRuler(new \Alexya\Validator\Rulers\StringRuler()); $validator->add("username", "test") ->addRule("String::required", "Please, enter your username") ->addRule("String::min_length", [3], "Your username can't be less than 3 chars") ->addRule("String::max_length", [20], "Your username can't be more than 20 chars") ->addRule("String::not_contains_chars", ["#$!\\"], "Your password can't contain special chars"); $validator->add("password", "asdf123") ->addRule("String::required", "Please, enter your password") ->addRule("String::min_length", [3], "Your password can't be less than 3 chars"); if($validator->validate()) { echo "All input has been successfully validated" } else { $messages = $validator->getErrors(); echo "Couldn't validate inputs!"; foreach($messages as $message) { echo $message; } }
The class \Alexya\Validator\Field
represents the field to validate., (*15)
The constructor accepts as parameter the field to validate., (*17)
You can add validation rules with the method addRule
which accepts three parameters:, (*19)
This method returns an instance of this object for chainability., (*20)
Once you've added all rules, use the method validate
to validate the field.
It accepts as parameter the ruler object with the validation rules and returns
true
if the validation succeeded, false
if not., (*22)
The method getErrors
returns an array with the validation errors., (*23)
Example:, (*24)
<?php $field = new Field("test"); $field->addRule("String::not_empty", "The string can't be empty!"); ->addRule("String::min_length", [4], "The string can't be shorter than 4 chars!"); ->addRule("String::max_length", [20], "The string can't be longer than 20 chars!"); ->addRule("String::matches", ["([0-9A-Z]*)"], "The string must have only numbers and letters!"); if(!$field->validate()) { echo implode("\n", $field->getErrors()); }
The class \Alexya\Validator\Ruler
is the base class for all rulers., (*26)
A ruler is an object that contains the methods for validating input., (*27)
The class \Alexya\Validator\Rulers\StringRuler
contains validation rules for strings., (*29)
Validation Rules:, (*30)
Rule | Parameters | Description |
---|---|---|
not_empty | Requires the value to not be empty. | |
min_length | int $i |
Requires the value to be, at least, $i length. |
max_length | int $i |
Requires the value to be less than $i length. |
length_between |
int $min , int max
|
Requires the value to length to be between $min and $max . |
contains_chars | string/array $chars |
Requires the value to contain any of the specified chars. |
matches | string $regex |
Requires the value to match $regex . |
is_email | Requires the value to be a valid email. | |
is_url | Requires the value to be a valid url. | |
is_ip | Requires the value to be a valid IP. | |
is_mac | Requires the value to be a valid mac. | |
is_regex | Requires the value to be a valid regex. | |
is_hash | Requires the value to be a valid hash. | |
is_json | Requires the value to be a valid json. |
All the rules can start with String::
., (*31)
Example:, (*32)
<?php $ruler = new \Alexya\Validator\Rulers\StringRuler(); var_dump($ruler->validate("String::not_empty", "")); // bool(false); var_dump($ruler->validate("min_length", "test", [4])); // bool(true); var_dump($ruler->validate("String::is_ip", "127.0.0.1")); // bool(true);
The class \Alexya\Validator\Rulers\IntegerRuler
contains validation rules for integers., (*34)
Validation Rules:, (*35)
Rule | Parameters | Description |
---|---|---|
less_than | int $min |
Checks if $value is less than $min . |
more_than | int $min |
Checks if $value is more than $min . |
between |
int $min , int $max
|
Checks if $value is between than $min and $max . |
All the rules can start with Integer::
., (*36)
Example:, (*37)
<?php $ruler = new \Alexya\Validator\Rulers\IntegerRuler(); var_dump($ruler->validate("Integer::less_than", 20, [40])); // bool(false); var_dump($ruler->validate("more_than", 30, [4])); // bool(true); var_dump($ruler->validate("Integer::between", 40, [4, 400])); // bool(true);