Skip to content

Validations

Mehdi Bounya edited this page Aug 21, 2018 · 3 revisions

Validations are the set of rules used by the \PHPForms\Validator to validate data.

Content


Creating the \PHPForms\Validations object

To create the object you have to pass an $errors argument to the constructor:

$validations = new \PHPForms\Validations($errors);

The $errors argument is just an array of 'rule' => 'Error message', you can use the array available in src/errors.php.

Rules reference

Here are the rules available in \PHPForms\Validations:

Rule Description Arguments Example
required Make sure the field is not empty. N/A
inList Make sure the field exists in the passed list. Valid options separated by , inList[male,female]
minLength Make sure the field is not shorter than the passed length Minimum length minLength[8]
maxLength Make sure the field is not longer than the passed length Maximum length maxLength[32]
exactLength Make sure the field is exactly the passed length Required length exactLength[10]
greaterThan Make sure the field is greater than the passed value. If the field is not numeric the validation fails. The value to compare greaterThan[17]
greaterThanEq Same as greaterThan except it's greater than or equal The value to compare greaterThanEq[17]
lessThan Make sure the field is less than the passed value. If the field is not numeric the validation fails. The value to compare lessThan[100]
lessThanEq Same as lessThan except it's less than or equal The value to compare lessThan[100]
validEmail Make sure the field is a valid e-mail N/A

For example, if you want a field to be required and accept 'male' and 'female' only you will pass required|inList[male,female] to the rules option in the Form

Extending the rules

To add your own rules to the available rules you can extend the \PHPForms\Validations class with your own validations:

<?php

class CustomRules extends \PHPForms\Validations
{
    public function ruleName(string $value):bool
    {
        // Do the checkings
        // Return TRUE if the data is valid
    }

    public function ruleThatTakesArguments(string $value, string $args):bool
    {
        // Do things
        // Return TRUE if the data is valid
    }
}

ruleName is apparently the rule's name and it will be used in the rules options, the \PHPForms\Validator will pass the data to check to the $value parameter.

$args is optional if you want your rule to take arguments (like the inList rule for example), and you can pass it to the rules option in your Form like this ruleThatTakesArguments[argument].

Don't forget to add your rules' error messages to the existing ones.

Then you will have to pass it to the validator:

$validations = new CustomRules($errors);

$errors here is the same as here except that you will have to add extra errors for your rules.

Creating your own set of rules

If you want to create your own set of rules (not add them to the existing ones) it's the same as Extending the rules except that your Class must extend the \PHPForms\Validation abstract class instead of the \PHPForms\Validations class (the difference is the S).

<?php

class CustomRules extends \PHPForms\Validations
{
    ...
}

Error messages

Error messages are a simple array of 'rule_name' => 'Error message'. If you added your own rules you will have to provide the error messages like this:

$errors = [
    // If you extended the rules don't forget to append the error not create a new array
    'ruleName' => 'The {field} field did not respect the rule.',
];

{field} will be replaced with the field's Label if it exists or the field name if no Label was found, there is also {params} which will be replaced with arguments passed to the rule name. e.g.:

in inList[option1,option2], {params} will be replaced with option1,option2.

Clone this wiki locally