How to use validations in Laravel

How to use validations in Laravel

In this article lets understand how to use validations in laravel.

We can use validations for checking incoming HTTP request before we will actually use them to process. Let’s see below how to do that.

You can use validation in laravel with two different ways:

  1. In your controller
  2. Separate request file for validations

1) In your controller:

This method is recommended when you have less logic for validation and smaller requests. Such as only few fields from the requests needs to be validated.

Let’s check the code below which we write within our method itself where we are receiving HTTP request.

use Illuminate\Http\Request;
public function store(Request $request)
    $validatedData = $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|email|unique:users|max:255'
    // Process the validated data

As you can see from above we use validate() method where we will define separate rules for each of our request fields.

  • Use validate() method
  • Use “|” to separate the rule
  • required: The field must be present and not empty.
  • string: The field must be a string.
  • max:255: The maximum length of the string is 255 characters.
  • email: The field must be a valid email format.
  • unique:users: The email field must be unique in the “users” table.
  • min:8: The field must have a minimum length of 8 characters.

These are various rules and its specification. This fields will have default messages for its failure. In case you want to edit the validation messages for any rule you can also define them as below:

$validatedData = $request->validate([
    'email' => 'required|email|unique:users|max:20'
], [
    'email.required' => 'Email field is necessary.',
    '' => 'Email field should have specific email format..',
    'email.unique' => 'Email address is not valid. Try different one.',
    'email.max' => 'Email too long.',

So this way we can define more meaningful messages in our own way.

2) Separate request file for validation:

Now in most cases we will be having too many fields to validate such as forms or big api requests etc.

Keeping your code clean and having rules defined separately is always a recommended way to use lravel validations.

Laravel provides a artisan way to generate requests using command. Lets create request using below command :

php artisan make:request StoreUserRequest

Now under your app/http/ folder a new file StoreUserRequest.php will be created. Use the rules method within file to define rules as below:

public function rules()
    return [
        'name' => 'required|string|max:255',
        'email' => 'required|email|unique:users|max:255',

Then in your controller you can use this request files as :

use App\Http\Requests\StoreUserRequest;

public function store(StoreUserRequest $request)
 //Here you can access the validated data directly using $request->validated()
 // proceed here

So, these were the basic only of Laravel validations. There are various rules supported by Laravel and various ways to manipulate the data. Checkout Laravel official documentation for that.

That’s all on how to use validations in Laravel Hope this finds you helpful.

See other articles on Laravel here.