Aplus Framework Docs

Validation

Aplus Framework Validation Library

Aplus Framework Validation Library.

Installation

The installation of this library can be done with Composer:

composer require aplus/validation

Basic Usage

Validation logic typically occurs as follows:

use Framework\Validation\Validation;

$validation = new Validation();
$validation->setRule('email', 'required|email'); // static

$validated = $validation->validate($_POST); // bool

if ($validated) {
    echo 'Validated!';
} else {
    echo 'Invalid data:';
    echo '<ul>';
    foreach ($validation->getErrors() as $error) {
        echo "<li>{$error}</li>";
    }
    echo '</ul>';
}

First load the Validation class. Then the rules are set and finally validated. Then a response is shown if the validation was valid or not.

Setting Rules

Rules can be set individually by the setRule method or several at once by setRules. The first argument is the name of the field and the second is the rules, which can be defined by string separating them with a pipe or by having an array of rules as values.

$validation->setRule('email', 'required|email'); // static
$validation->setRule('firstname', ['required', 'minLength:2']); // static

$validation->setRules([
    'lastname' => 'required|minLength:2|maxLength:32'
]); // static

Setting Labels

Error messages show field name as default. And often you need to show a custom label like First Name instead of firstname.

Labels can be defined individually or by an array.:

$validation->setLabel('email', 'E-mail'); // static
$validation->setLabel('firstname', 'First Name'); // static
// or
$validation->setLabels([
    'email' => 'E-mail',
    'firstname' => 'First Name',
]); // static

Getting Errors

Errors can be obtained individually or all at once, as per the example below:

// Email field error message, or null
$error = $validation->getError('email'); // string or null

// All errors
$errors = $validation->getErrors(); // array

Validating

After defining the rules and labels, the validation of the received data occurs through the validate method.

If you only need to validate the received fields, you can use the validateOnly method. Useful for updating only a few fields in the database.

// Validates all fields
$validated = $validation->validate($data); // bool

// Validates only received fields
$validated = $validation->validateOnly($data); // bool

Validator Check

To validate only one field is possible to use only the Validator:

use Framework\Validation\Validator;

$validated = Validator::alpha('name', $data); // bool

Working with Arrays

Validator uses the ArraySimple class to extract fields and get the correct data value.

use Framework\Validation\Validation;

$validation = new Validation();
$validation->setLabel('user[pass]', 'Password') // static
           ->setRule('user[pass]', 'required'); // static

$data = [
    'user' => [
        'pass' => 'secret',
    ],
];

$validated = $validation->validate($data); // true

Custom Validator

It is possible to create a validator with your custom rules.

use Framework\Validation\Validator;

class CustomValidator extends Validator
{
    public static function phone(string $field, array $data): bool
    {
        $data = static::getData($field, $data);
        if ($data === null) {
            return false;
        }
        return \preg_match('/^\d{4}-\d{4}$/', $data);        
    }
}

Do not forget to create the validation language file with your rules.

File Languages/en/validation.php:

return [
    'phone' => 'The {field} field requires a valid phone number.'
];

So, let the Validation know about your customizations:

use CustomValidator;
use Framework\Language\Language;
use Framework\Validation\Validation;

$language = new Language();
$language->addDirectory(__DIR__ . '/Languages');

$validation = new Validation([CustomValidator::class], $language);

$validation->setRule('telephone', 'required|phone'); // static

$validated = $validation->validate($_POST); // bool

$errors = $validation->getErrors(); // array

Available Rules

The available rules are:

alpha

The field requires only alphabetic characters.

alpha

alphaNumber

The field requires only alphabetic and numeric characters.

alphaNumber

array

The field requires an array.

array

base64

The field requires a valid base64 string.

base64

between

The field must be between {0} and {1}.

between:$min,$max

The rule must take two parameters: $min and $max.

$min is the minimum value.

$max is the maximum value.

blank

If the field has a blank string, the validation passes.

blank

bool

The field requires a boolean value.

bool

datetime

The field must match a required datetime format.

datetime
datetime:$format

The rule can take one parameter: $format.

$format is the date format.

By default the format is Y-m-d H:i:s.

dim

The field requires an image with the exact dimensions of {0} in width and {1} in height.

dim:$width,$height

The rule must take two parameters: $width and $height.

$width is the exact width of the image.

$height is the exact height of the image.

email

The field requires a valid email address.

email

empty

If the field is defined and has an empty value, the validation passes.

empty

equals

The field must be equals the {0} field.

equals:$equalsField

The rule must take one parameter: $equalsField.

$equalsField is the name of the field which must be equal to this one.

ext

The field requires a file with an accepted extension: {args}.

ext:...$allowedExtensions

The rule can take several parameters: ...$allowedExtensions.

...$allowedExtensions is a comma-separated list of file extensions.

float

The field requires a floating point number.

float

greater

The field must be greater than {0}.

greater:$greaterThan

The rule must take one parameter: $greaterThan.

$greaterThan is the value the field must be greater than this.

greaterOrEqual

The field must be greater than or equal to {0}.

greaterOrEqual:$greaterThanOrEqualTo

The rule must take one parameter: $greaterThanOrEqualTo.

$greaterThanOrEqualTo is the value that the field has greater than or equal to this.

hex

The field requires a valid hexadecimal string.

hex

hexColor

The field requires a valid hexadecimal color.

hexColor

image

The field requires an image.

image

in

The field must have one of the listed values.

in:$in,...$others

The rule must take one parameter: $in. And also ...$others.

$in is a value required to be in.

...$others are other valid values to be in.

int

The field requires an integer.

int

ip

The field requires a valid IP address.

ip
ip:$version

The rule can take one parameter: $version.

$version can be 0 for IPv4 and IPv6. 4 for IPv4 or 6 for IPv6.

isset

The field must be sent.

isset

json

The field requires a valid JSON string.

json

latin

The field requires only latin characters.

latin

length

The field requires exactly {0} characters in length.

length:$length

The rule can take one parameter: $length.

$length is the exact number of characters the field must receive.

less

The field must be less than {0}.

less:$lessThan

The rule can take one parameter: $lessThan.

$lessThan is the value that the field has less than this.

lessOrEqual

The field must be less than or equal to {0}.

lessOrEqual:$lessThanOrEqualTo

The rule can take one parameter: $lessThanOrEqualTo.

$lessThanOrEqualTo is the value that the field has less than or equal to this.

maxDim

The field requires an image that does not exceed the maximum dimensions of {0} in width and {1} in height.

maxDim:$width,$height

The rule can take two parameters: $width and $height.

$width is the maximum width the image can be.

$height is the maximum height the image can be.

maxLength

The field requires {0} or less characters in length.

maxLength:$maxLength

The rule can take one parameter: $maxLength.

$maxLength is the maximum amount of characters that the field must receive.

maxSize

The field requires a file that does not exceed the maximum size of {0} kilobytes.

maxSize:$kilobytes

The rule can take one parameter: $kilobytes.

$kilobytes is the maximum number of kilobytes that the field file can receive.

md5

The field requires a valid MD5 hash.

md5

mimes

The field requires a file with an accepted MIME type: {args}.

mimes:...$allowedTypes

The rule can take many parameters: ...$allowedTypes.

...$allowedTypes are the MIME types of files the field can receive.

minDim

The field requires an image having the minimum dimensions of {0} in width and {1} in height.

minDim:$width,$height

The rule can take two parameters: $width and $height.

$width is the minimum width the image can be.

$height is the minimum height the image can be.

minLength

The field requires {0} or more characters in length.

minLength:$minLength

The rule can take one parameter: $minLength.

$minLength is the minimum number of characters the field must receive.

notBetween

The field can not be between {0} and {1}.

notBetween:$min,$max

The rule can take two parameters: $min and $max.

$min is the minimum value that the field value must not have.

$max is the maximum value the field value must not have.

notEquals

The field can not be equals the {0} field.

notEquals:$diffField

The rule can take one parameter: $diffField.

$diffField is the name of the field that must have a value different from this one.

notIn

The field must have a value other than those listed.

notIn:$notIn,...$others

The rule can take one parameter: $notIn. And also ...$others.

$notIn is the value required not to be in.

...$others are other values to not be in.

notRegex

The field matches a invalid pattern.

notRegex:$pattern

The rule can take one parameter: $pattern.

$pattern is the regular expression that the field value must not match.

null

If the field value is null, the validation passes.

null

number

The field requires only numeric characters.

number

object

The field requires an object.

object

optional

The field is optional. If undefined, validation passes.

optional

regex

The field must match the required pattern.

regex:$pattern

The rule can take one parameter: $pattern.

$pattern is the regular expression that the value of the field must match.

required

The field is required.

required

slug

The field requires a valid slug.

slug

specialChar

The field requires special characters.

specialChar
specialChar:$quantity
specialChar:$quantity,$characters

The rule can take two parameters:: $quantity and $characters.

$quantity is the number of special characters the field value must have. By default the value is 1.

$characters are the characters considered special. By default they are these: !"#$%&\'()*+,-./:;=<>?@[\]^_`{|}~.

string

The field requires a string.

string

timezone

The field requires a valid timezone.

timezone

uploaded

The field requires a file to be uploaded.

uploaded

url

The field requires a valid URL address.

url

uuid

The field requires a valid UUID.

uuid

Conclusion

Aplus Validation Library is an easy-to-use tool for, beginners and experienced, PHP developers.
It is perfect for validating data coming from a form or API.
The more you use it, the more you will learn.

Did you find something wrong?
Be sure to let us know about it with an issue.
Thank you!

Search results