Laravel

Laravel Form Request Validation

Laravel Form Request Validation
Written by ProgrammingPot

In this example, we are using a Laravel Form Request Validation on the request object, then applying rules to each of our form inputs. If this passes the rules we set up then we will create our new Market and redirect back to the markets index page.

This type of system works fine when we have a couple of fields, but when our form grows, so will our validation. We want to also keep our code clean and reusable, for instance when we will need to call the same validation from our edit action.

Laravel Form Request Validation


We will handle this by using a custom Form Request Validation which we will need to generate. To accomplish this you will want to run the artisan make:request command followed by the name of the request.

Creating Laravel Form Request Validation

For more complex validation scenarios, you may wish to create a “form request”. Form requests are custom request classes that contain validation logic. To create a form request class, use the make:request Artisan CLI command:

php artisan make:request UserRequest

The generated class will be placed in the directory. app/Http/Requests If this directory does not exist, it will be created when you run the make:request command. Let’s add a few validation rules to the rules method:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest 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 [
            //
        ];
    }
}

So, how are the validation rules evaluated? All you need to do is type-hint the request on your controller method. The incoming form request is validated before the controller method is called, meaning you do not need to clutter your controller with any validation logic:

/**
 * Store the incoming blog post.
 *
 * @param  StoreBlogPost  $request
 * @return Response
 */
public function store(UserRequest $request)
{
    // The incoming request is valid...

    // Retrieve the validated input data...
    $validated = $request->validated();
}

Authorizing Form Requests

The Laravel Form Request Validation class also contains an authorize method. Within this method, you may check if the authenticated user actually has the authority to update a given resource.

For example, you may determine if a user actually owns a blog comment they are attempting to update:

If the authorize method returns false, a HTTP response with a 403 status code will automatically be returned and your controller method will not execute.

If you plan to have authorization logic in another part of your application, return true from the authorize method:

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

Customizing The Error Messages

You may customize the error messages used by the form request by overriding the messages method. This method should return an array of attribute / rule pairs and their corresponding error messages:

/**
 * Get the error messages for the defined validation rules.
 *
 * @return array
 */
public function messages()
{
    return [
        'title.required' => 'A User Name is required',
        'email.required'  => 'A Email is required',
    ];
}

For more view the Laravel Simple CRUD Operation, in this post you will find the proper uses of laravel custom validation.


Leave a Comment