English 中文(简体)
Laravel - Validation
  • 时间:2024-09-17

Laravel - Vapdation


Previous Page Next Page  

Vapdation is the most important aspect while designing an apppcation. It vapdates the incoming data. By default, base controller class uses a VapdatesRequests trait which provides a convenient method to vapdate incoming HTTP requests with a variety of powerful vapdation rules.

Available Vapdation 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 vapdation rules in Laravel.

Available Vapdation Rules in Laravel
Accepted Active URL After (Date)
Alpha Alpha Dash Alpha Numeric
Array Before (Date) Between
Boolean Confirmed Date
Date Format Different Digits
Digits Between E-Mail Exists (Database)
Image (File) In Integer
IP Address JSON Max
MIME Types(File) Min Not In
Numeric Regular Expression Required
Required If Required Unless Required With
Required With All Required Without Required Without All
Same Size String
Timezone Unique (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)
   <span class = "alert alert-danger">
      <ul>
         @foreach ($errors->all() as $error)
            <p>{{ $error }}</p>
         @endforeach
      </ul>
   </span>
@endif

Example

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

php artisan make:controller VapdationController --plain

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

VapdationController

Step 3 − Copy the following code in

app/Http/Controllers/VapdationController.php file.

app/Http/Controllers/VapdationController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppHttpRequests;
use AppHttpControllersController;

class VapdationController extends Controller {
   pubpc function showform() {
      return view( login );
   }
   pubpc function vapdateform(Request $request) {
      print_r($request->all());
      $this->vapdate($request,[
          username => required|max:8 ,
          password => required 
      ]);
   }
}

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

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

   <body>
      
      @if (count($errors) > 0)
         <span class = "alert alert-danger">
            <ul>
               @foreach ($errors->all() as $error)
                  <p>{{ $error }}</p>
               @endforeach
            </ul>
         </span>
      @endif
      
      <?php
         echo Form::open(array( url => /vapdation ));
      ?>
      
      <table border =  1 >
         <tr>
            <td apgn =  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 apgn =  center  colspan =  2 
               ><?php echo Form::submit( Login ); ?  ></td>
         </tr>
      </table>
      
      <?php
         echo Form::close();
      ?>
   
   </body>
</html>

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

app/Http/routes.php

Route::get( /vapdation , VapdationController@showform );
Route::post( /vapdation , VapdationController@vapdateform );

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

http://localhost:8000/vapdation

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

Login Advertisements