Laravel - Validation
By default, base controller class utilizes a ValidatesRequests quality which gives a helpful technique to approve approaching HTTP asks for with an assortment of incredible approval rules.
Available Validation Rules in Laravel
note that a $errors variable will dependably be accessible in the majority of your perspectives on each demand, enabling you to helpfully expect the $errors variable is constantly characterized and can be securely utilized.Available Validation 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 | 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 |
@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 accompanying command.
php craftsman make:controller ValidationController - plain
Step 2 − After effective execution, you will get the accompanying yield −
Step 3 − Copy the accompanying code in
app/Http/Controllers/ValidationController.php file.
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 record called resources/sees/login.blade.php and duplicate the accompanying code in that file.
resources/sees/login.blade.php
<html> <head> <title>Login Form</title> </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php reverberation Form::open(array('url'=>'/validation')); ?> <table border = '1'> <tr> <td align = 'center' colspan = '2'>Login</td> </tr> <tr> <td>Username</td> <td><?php reverberation Form::text('username'); ?></td> </tr> <tr> <td>Password</td> <td><?php reverberation Form::password('password'); ?></td> </tr> <tr> <td align = 'center' colspan = '2' ><?php reverberation Form::submit('Login'); ? ></td> </tr> </table> <?php reverberation Form::close(); ?> </body> </html>
Step 5 − Add the accompanying lines in app/Http/routes.php.
app/Http/routes.php
Route::get('/validation','ValidationController@showform'); Route::post('/validation','ValidationController@validateform');
Step 6 − Visit the accompanying URL to test the validation.
http://localhost:8000/approval
Step 7 − Click the Login catch without entering anything in the content field. The yield will be as appeared in the accompanying image.