ValidatorTrait uses trait:short
Provides helper methods for validation and error handling within a controller.
Trait ValidatorTrait
Tags
Table of Contents
Properties
- $customRules : array<string|int, mixed>
- The custom validation rules definitions.
- $rules : array<string|int, mixed>
- The rules definitions used in the prepareRules method to initialize a validation process in the POST/PATCH/PUT and custom methods.
- $validator : Factory
- The validator reference.
Methods
- addRules() : void
- Register the extra validator's rules.
- fail() : ResponseInterface|null
- Formats a specific error status message with a code and an errors array representation of all errors.
- initializeValidator() : static
- Sets the current internal validator of the controller.
- status() : ResponseInterface|null
- Outputs a response status message.
- success() : mixed
- Outputs a success message with a JSON response. If the $response parameter is null, returns the $data parameter value.
- getValidatorError() : ResponseInterface|null
- Returns an error if the validator fails.
- initCustomValidationRules() : array<string|int, mixed>
- Returns the list of all extra-rules to initialize the validator.
- prepareRules() : array<string|int, mixed>
- Merge the default common and the specific method's rules.
Properties
$customRules
The custom validation rules definitions.
public
array<string|int, mixed>
$customRules
= []
$rules
The rules definitions used in the prepareRules method to initialize a validation process in the POST/PATCH/PUT and custom methods.
public
array<string|int, mixed>
$rules
= []
Tags
$validator
The validator reference.
protected
Factory
$validator
Methods
addRules()
Register the extra validator's rules.
public
addRules([array<string|int, mixed> $rules = [] ]) : void
Parameters
- $rules : array<string|int, mixed> = []
Tags
fail()
Formats a specific error status message with a code and an errors array representation of all errors.
public
fail(ResponseInterface|null $response[, int|string|null $code = 400 ][, string|null $details = null ][, array<string|int, mixed> $options = [] ]) : ResponseInterface|null
Ex: A 'not acceptable' http request with a failed validation process
return $this->getError( $response , 406 , 'fields validation failed' , [ 'firstName' => 'firstName is required' , 'lastName' => 'lastName must be a string' ] ] ) ;
``
Parameters
- $response : ResponseInterface|null
-
The Response reference.
- $code : int|string|null = 400
-
The status code of the response.
- $details : string|null = null
-
The optional error message to overrides the default status message.
- $options : array<string|int, mixed> = []
-
The optional array to inject in the json object (with an errors)
Return values
ResponseInterface|nullinitializeValidator()
Sets the current internal validator of the controller.
public
initializeValidator([null|array<string|int, mixed>|Factory $init = null ]) : static
By default, creates a new Validator instance and initialize it.
Parameters
- $init : null|array<string|int, mixed>|Factory = null
Tags
Return values
staticstatus()
Outputs a response status message.
public
status(ResponseInterface|null $response[, mixed $message = Char::EMPTY ][, int|string|null $code = 200 ][, array<string|int, mixed>|null $options = null ]) : ResponseInterface|null
Parameters
- $response : ResponseInterface|null
- $message : mixed = Char::EMPTY
-
The message to send.
- $code : int|string|null = 200
-
The status code.
- $options : array<string|int, mixed>|null = null
-
The options to passed-in in the status definition.
Tags
Return values
ResponseInterface|nullsuccess()
Outputs a success message with a JSON response. If the $response parameter is null, returns the $data parameter value.
public
success(ServerRequestInterface|null $request, ResponseInterface|null $response[, mixed $data = null ][, array<string|int, mixed>|null $init = null ]) : mixed
Ex: return $this->success( $request , $response , $data , [ Output::PARAMS => $request->getParams() ] ) ;
Parameters
- $request : ServerRequestInterface|null
-
The HTTP request reference.
- $response : ResponseInterface|null
-
The HTTP Response reference.
- $data : mixed = null
-
The data object to returns (output a JSON object).
- $init : array<string|int, mixed>|null = null
-
An associative definition to initialize the output object with the optional properties :
-
count (int) - The optional number of elements.
owner (object|array) - The optional owner reference.
options (array) - An associative array of optional properties to add in the output object.
params (array) - The optional params to passed-in the getCurrentPath() method when the url option is null.
status (int) - The optional status of the response.
total (int) - The optional total number of elements.
url (string) - The optional url to display.
getValidatorError()
Returns an error if the validator fails.
protected
getValidatorError(ResponseInterface|null $response, Validation $validation[, array<string|int, mixed> $errors = [] ][, int|string $code = 400 ]) : ResponseInterface|null
Parameters
- $response : ResponseInterface|null
- $validation : Validation
- $errors : array<string|int, mixed> = []
- $code : int|string = 400
Return values
ResponseInterface|nullinitCustomValidationRules()
Returns the list of all extra-rules to initialize the validator.
protected
initCustomValidationRules() : array<string|int, mixed>
Overrides this method to extends the default rules definitions.
Return values
array<string|int, mixed>prepareRules()
Merge the default common and the specific method's rules.
protected
prepareRules([string|null $method = null ]) : array<string|int, mixed>
You can overrides this method to prepare the validator rules with a specific router method and strategy.
Parameters
- $method : string|null = null
-
The specific rule type to override the default rules definitions.