Loading

Laravel

How to use Validation in Laravel?. The Complete Laravel Developer Course 2023 [Videos].

Validation is the most important aspect while designing an application. It validates the incoming data. By default, base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules.

Available Validation Rules in Laravel

Laravel will always check for errors in the session data, and automatically bind them to the view if they are available. So, it is important to note that a $errors variable will always be available in all of your views on every request, allowing you to conveniently assume the $errors variable is always defined and can be safely used. The following table shows all available validation rules in Laravel.

Available Validation Rules in Laravel
AcceptedActive URLAfter (Date)
AlphaAlpha DashAlpha Numeric
ArrayBefore (Date)Between
BooleanConfirmedDate
Date FormatDifferentDigits
Digits BetweenE-MailExists (Database)
Image (File)InInteger
IP AddressJSONMax
MIME Types(File)MinNot In
NumericRegular ExpressionRequired
Required IfRequired UnlessRequired With
Required With AllRequired WithoutRequired Without All
SameSizeString
TimezoneUnique (Database)URL

The $errors variable will be an instance of IlluminateSupportMessageBag. Error message can be displayed in view file by adding the code as shown below.

@if (count($errors) > 0)
   <div class = "alert alert-danger">
      <ul>
         @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
         @endforeach
      </ul>
   </div>
@endif

Example

Step 1 − Create a controller called ValidationController by executing the following command.

php artisan make:controller ValidationController --plain

Step 2 − After successful execution, you will receive the following output −

ValidationController

Step 3 − Copy the following code in

app/Http/Controllers/ValidationController.php file.

app/Http/Controllers/ValidationController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

Step 4 − Create a view file called resources/views/login.blade.php and copy the following code in that file.

resources/views/login.blade.php


Step 5 − Add the following lines in app/Http/routes.php.

app/Http/routes.php

Step 6 − Visit the following URL to test the validation.

http://localhost:8000/validation

Step 7 − Click the “Login” button without entering anything in the text field. The output will be as shown in the following image.

Login


See All

Comments (496 Comments)

Submit Your Comment

See All Posts

Related Posts

Laravel / Blog

How to install Laravel ?

For managing dependencies, Laravel uses composer. Make sure you have a Composer installed on your system before you install Laravel. In this chapter, you will see the installation process of Laravel.
14-Feb-2022 /15 /496

Laravel / Blog

What is Laravel - Application Structure?

The application structure in Laravel is basically the structure of folders, sub-folders and files included in a project. Once we create a project in Laravel, we get an overview of the application structure as shown in the image here.
14-Feb-2022 /15 /496

Laravel / Blog

How to setup Configuration in Laravel?

n the previous chapter, we have seen that the basic configuration files of Laravel are included in the config directory. In this chapter, let us discuss the categories included in the configuration.
14-Feb-2022 /15 /496