How do I create custom validation requests?

To create a custom validation request, we need to extend the AeroRequest abstract class that’s located in aerocommerce/core.

To create a custom request, open the terminal in the project root directory and type the following command:

php artisan make:request MyRequest

The above command generates a class file that we now have to modify to suit the needs of both AeroRequest and the data we’re working with. The first change is the class our brand new request extends, instead of FormRequest, we now need the class to extend AeroRequest from namespace App\Http\Requests.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class MyRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

The authorize() function is a way of checking whether the current user can use the request we have just created. The best way of checking if the user currently handling the request has the correct permissions. This can be checked by writing the following code in the authorize() method:

$address = $this->route('address');

return $address && $this->user()->can('update', $address);

The above code checks if a user has been permitted to update for a given route, in this case, the address.

The rules() function allows creating all the necessary rules for fields in a given request. The Laravel documentation on validation showcases ways of writing validation rules, alongside mentioning several useful dev tips.

 

Adding rules

If you wish to add rules to a brand new validation request, all we have to do is populate the array in the return of the rules() method.

For example, if we wanted to add validation rules for a basic contact form, they’d look like this:

**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
 public function rules(): array
 {
     return [
         'first_name' => 'required|string|max:50',
         'last_name' => 'required|string|max:50',
         'email' => ‘required|email',
         'message' => 'required|string',
     ];
 }

 

Adding messages

The messaged method will not be automatically scaffolded when creating a new request and we have to do this manually. To add custom messages for our validation errors, we simply create a new method under rules(), called messages() like so:

public function messages(): array
{
    return parent::messages(); // TODO: Change the autogenerated stub
}

If we wish to set custom validation messages, we have to specify the fields and the rule we wish to display messaged for like so;

public function messages(): array
{
    return [
        'email.required' => 'The email address is required!',
    ];
}

Articles in this section

Was this article helpful?
0 out of 0 found this helpful