Data Validation
Incoming data is always automatically validated based on the endpoint parameters if any.
Auto-validation only ensures that the request matches the desired model structure, if the data itself is valid or not still needs to be validated by the developer.
Take the following controller code that handles logic for a simple todo app:
To validate our todo entries, we will use the Dominus\Services\Validator
service to help us.
We start by injecting the Dominus\Services\Validator
service in our add
method, then use the validate
method to validate the data using the given rules.
The validate
method accepts an array of the form:
Data model validation
You can also validate data model properties when they are mapped from the Request object.
Let's take our FormDataModel
from the previous example and setup some validation for its properties:
Available validation rules
Below is a list of all the available validation rules.
Rules are separated using the pipe character |
Rule arguments are separated by a semicolon: :
.
Example: min_length:5|max_length:200
- min_length
- max_length
- in_list
- not_in_list
- true
- not_equals
- equals
- required
- date
- date_equals
- date_after
- date_after_or_equal
- date_before
- date_before_or_equal
min_length
min_length:[length]
Verifies that the field string length is greater than or equal to the given length.
Positional Arguments:
* [Required] the minimum length. Example: min_length:10
max_length
max_length:[length]
Verifies that the field string length is less than or equal to the given length.
Positional Arguments:
* [Required] the maximum length. Example: min_length:200
in_list
in_list:comma, separated, items
Verifies that the field value is contained in the given list.
Positional Arguments:
* [Required] A list of items to check the validated field against. Example: in_list:item1, item2, item3
not_in_list
not_in_list:comma, separated, items
Verifies that the field value is not contained in the given list.
Positional Arguments:
* [Required] A list of items to check the validated field against. Example: in_list:item1, item2, item3
is_true
is_true
Verifies that the field value has a true
boolean value.
is_false
is_false
Verifies that the field value has a false
boolean value.
equals
equals:[value]
Verifies that the field value equals the provided static value.
Positional Arguments:
* [Required] The value to check the validated field against. Example: equals:27
not_equals
not_equals:[value]
Verifies that the field value does not equal the provided static value.
Positional Arguments:
* [Required] The value to check the validated field against. Example: not_equals:27
email
Verifies if the email is well formatted, uses php's filter_var
function.
If you need more advanced validation, you may want to use a custom validator.
required
required
Verifies that the field exists and is not empty.
date
date:[format]
The validated field value will be verified by using the PHP strtotime
function.
Positional Arguments:
* [Optional] format to validate against. Example: date:Y-m-d
date_equals
date_equals:[datetime]:[format]
The validated field must be equal to the given date. The dates will be parsed and validated by the PHP strtotime
function.
Positional Arguments:
* [Optional] a string that can be parsed by the PHP strtotime
function. Example: date_equals:+2 days
* [Optional] compares the dates using the given date format. Example: date_equals:+2 days:Y-m-d H\:i
date_after
date_after:[datetime]
The validated field must be a value after the given date. The dates will be parsed and validated by the PHP strtotime
function.
Positional Arguments:
* [Optional] a string that can be parsed by the PHP strtotime
function. Example: date_after:now
date_after_or_equal
date_after_or_equal:[datetime]
The validated field must be a value after or equal to the given date. The dates will be parsed and validated by the PHP strtotime
function.
Positional Arguments:
* [Optional] a string that can be parsed by the PHP strtotime
function. Example: date_after_or_equal:2027-06-07
date_before
date_before:[datetime]
The validated field must be a value preceding the given date. The dates will be parsed and validated by the PHP strtotime
function.
Positional Arguments:
* [Optional] a string that can be parsed by the PHP strtotime
function. Example: date_before:2002-06-07
date_before_or_equal
date_before_or_equal:[datetime]
The validated field must be a value preceding or equal to the given date. The dates will be parsed and validated by the PHP strtotime
function.
Positional Arguments:
* [Optional] a string that can be parsed by the PHP strtotime
function. Example: date_before_or_equal:2002-06-07