Validate Data — Laravel 10 Discovery Training
In this new chapter we will discover the validation part before attacking the management of forms. It is important to make sure that the data sent to our application corresponds to what we expect. For this, Laravel offers us a class Validator
which will allow us to manage this operation.
$validator = Validator::make($data, [
'name' => 'required|string'
])
The method make()
will take as first parameter the data to be validated and as second parameter an array representing the validation rules.
There are several ways to define validation rules:
- In the form of a character string with the different rules separated by
|
. - In the form of a table listing all the rules (ex:
["required", "min:4"]
) - Using the class
Illuminate\Validation\Rule
for more complex rules (ex:Rule::dimensions()->maxWidth(1000)->maxHeight(500)->ratio(3 / 2)
)
From this object we can test the state of our validation using different methods.
- The method
fails()
returns a boolean to know if a validation failed or not - The method
errors()
allows you to retrieve error messages associated with elements that do not satisfy the validation rules. - The method
validated()
returns an array corresponding to the data that has been validated (removing the keys that have no validation rules).
This last method will also throw an exception if the data is not valid. This type of exception will be automatically captured by the framework which will react differently depending on the type of request.
- In the case of a request that expects JSON, errors will be returned in JSON format.
- In the case of a request that expects HTML, the response will be a redirect to the previous page with errors and submitted data saved in session.
This method validated()
is very interesting because it allows both to ensure that the data is valid for the rest of the execution and allows the framework to handle errors if necessary.
$data = $validator->validated();
// Pour récupérer juste une valeur
$firstname = $validator->validated('firstname');
FormRequest
If you want to validate the parameters coming from the request made by the user (submission of a form or validation of parameters in the URL) it is possible to use personalized request objects. To create a query you can use the artisan command:
php artisan make:request CreatePostRequest
This command will automatically create a new class which has 2 methods:
authorize
which will return a boolean to say whether or not the user has the right to consult the page (we will see this later).rules
which will return an array containing the validation rules to satisfy.
Then we can inject this object CreatePostRequest
in our controller, instead of the object Request
basic.
function store(CreatePostRequest $request) {
$data = $request->validated();
// ...
}
Laravel will automatically take care of validating the data from our rules and returning the correct answer in case of error. Inside the logic of the function we will know that the data is valid and we can continue its execution.