Início / Optitravel / Helpers / ValidatorHelper
validate(array $params, array $rules, string $protocol = '')
- validate(array $params, array $rules, string $protocol = '')
- Parameters
- Supported Rules
- Rules Examples
- Application Example
This method is used to validate input data against a set of rules and automatically returns an error response if validation fails. It ensures standardized request validation across the application and simplifies controller logic.
It is commonly used at the beginning of controller methods to check required fields, data types, lengths, and database existence before proceeding with any logic.
Parameters
-
$params – (array) Required. The data to be validated.
-
$rules – (array) Required. The rules to apply on each field, using a string with | separators (e.g., "mandatory|string|min:3").
-
$protocol – (string) Optional. The response format protocol (e.g., 'SCIM') for error output customization.
Supported Rules
Below is a list of validation rules you can use in the $rules array. Multiple rules can be combined using the | character (e.g., "mandatory|string|min:3").
-
Basic Rules:
-
mandatory - Field must be present and not empty.
- optional - Field is not required. If not present or empty, other rules are skipped.
- int - Field must be a valid integer.
- string - Field must be a string.
-
email - Field must be a valid email address.
-
Length and Value Rules
-
min:X - Minimum value (for numbers) or minimum length (for strings).
-
max:X - Maximum value (for numbers) or maximum length (for strings).
-
Database Rules:
-
exists:table,column - Checks if value exists in the given table and column using system DB.
- exists@optitravel:table,column - Same as above, but uses the central Optitravel DB connection.
- exists@db:table,column&field=value - Adds extra WHERE conditions to the existence check (AND clauses).
- not_exists:table,column - Checks if value does not exist (useful for uniqueness).
- not_exists@optitravel:table,column - Same as above but on the Optitravel DB.
- not_exists@db:table,column&field=value - Adds extra WHERE conditions to the non-existence check (AND clauses).
Rules Examples
1º Example:
'rules' => [
'systemId' => 'mandatory|exists@optitravel:systems,id'
]
Explanation: - This checks if systemId exists in the id column of the systems table in the optitravel database.
2º Example:
'rules' => [
'email' => "mandatory|email|not_exists@optitravel:users,email&system_id='Test'"
]
Explanation: - This ensures the email does not already exist in the users table, with an additional filter: system_id ='Test'.
3º Example:
'rules' => [
'username' => 'mandatory|string|min:3|max:20|not_exists:users,username',
'age' => 'optional|int|min:18',
'email' => 'mandatory|email|not_exists@optitravel:users,email&active=1'
]
Explanation: - username must be a string between 3–20 characters and not already used in users.username. - age is optional but, if present, must be an integer >= 18. - email must be valid and not exist in the optitravel.users table where active = 1.
Application Example
use \App\Utils\ValidatorHelper;
$params = [
'email' => 'invalid@email',
'age' => 16
];
$rules = [
'email' => 'mandatory|email',
'age' => 'mandatory|int|min:18'
];
ValidatorHelper::validate($params, $rules);
Output (if validation fails):
{
"error": true,
"code": 400,
"message": "Incorrect parameters",
"errors": {
"FIELD_MUST_VALID_EMAIL": [
{
"field": "email",
"description": "The field email must be a valid email address."
}
],
"FIELD_MIN_VALUE": [
{
"field": "age",
"description": "The field age must be at least 18."
}
]
}
}
↑ (Última atualização: 06/05/2025)