Laravel - Validation

Laravel - Validation

Validation is the most important aspect when developing an application. It checks the incoming data. By default, the base controller class uses the ValidatesRequests trait, which provides a convenient method for validating incoming HTTP requests with a variety of powerful validation rules.

Available validation rules in Laravel

Laravel always checks for errors in the session data and automatically associates it with the view if available. So it's important to note that the $errors variable will always be available in all your views on every request, which allows you to conveniently assume that the $errors variable is always defined and can be used safely. The following table lists all available validation rules in Laravel.

Available validation rules in Laravel
Accepted Active URL After (Date)
Alpha Alpha Dash Alpha Numeric
array To (Date) Between
logical confirmed date
Date format Different Digits
Numbers between Email mail Exists (database)
image file) V integer
IP address JSON Maximum
MIME Types (File) Min Not in
numerical Regular expression necessary
Required if Required only if Required since
Required with everyone Required without Required without everything
Same way The size line
Timezone Unique (Database) URL

The $errors variable will be an instance of Illuminate\Support\MessageBag . The error message can be displayed in the view file by adding code like 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 running the following command.

php artisan make:controller ValidationController --plain

Step 2 − Upon successful execution, you will get the following output −

Step 3 - Copy the following code into

The file is app/Http/Controllers/ValidationController.php .

app/Http/Controllers/ValidationController.php

<? php

namespace App \Http\Controllers ; 

use Illuminate \Http\Request ; use App \Http\Requests ; use App \Http\Controllers\Controller ; 
 
 

class ValidationController extends Controller { public function showform () { return view ( 'login' ); } public function validateform ( Request $request ) { 
      print_r ( $request -> all ()); 
      $this -> validate ( $request ,[ 'username' => 'required|max:8' , 'password' => 'required' ]); } }    
     

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

resources/views/login.blade.php

<html>
   
   <head> <title> Login Form </title> </head>
      
   

   <body>
      
      @if (count($errors) > 0)
         <div class = "alert alert-danger" > <ul>   
<li> {{ $error }} </li>
               @endforeach
            </ul> </div>
         
      @endif
      
      <? php
         echo Form :: open ( array ( 'url' => '/validation' )); ?>
      
      
      <table border = '1' > <tr> <td align = 'center' colspan = '2' > Login </td> </tr> <tr> <td> Username </td> <td> <? php echo Form :: text ( 'username' ); ?> </td> </tr> <tr> <td> Password </td> <td> <? php echo Form :: password ( ' password' ); ?> </td> </tr>   
         
                  
         
         
            
             
<tr> <td align = 'center' colspan = '2' > <? php echo Form :: submit ( 'Login' ); ? ></ td > </ tr > </ table >
                  
                  
         
      
      
      <? php
         echo Form :: close (); ?>
      
   
   </body> </html>

Step 5 - Add the following lines to app/Http/rout.php .

app/http/routes.php

Route::get('/validation','ValidationController@showform');
Route::post('/validation','ValidationController@validateform');

Step 6 − Visit the following URL to check if it is correct.

http://localhost:8000/validation

Step 7 − Click the Login button without typing anything in the text box. The output will be as shown in the following figure.

  @foreach ($errors->all() as $error)