- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use Validations in Laravel?
It is important to validate the data that comes from the HTML forms. Laravel provides you with the validate() method using which you can define the rules on the input fields the way you want.
The following examples show the working of validate() and how to make use of it on the HTML form input fields.
To test validation, create controller as shown below −
php artisan make:controller testvalidationController
You will get the following output once it is done −
C:\xampp\htdocs\laraveltest>php artisan make:controller testvalidationController Controller created successfully. C:\xampp\htdocs\laraveltest>
Open the controller “testvalidationController” and add validation rules for the form that has a username and password. The rule for validation on the form is defined as follows −
$this->validate($request,[ 'username'=>'required|max:8', 'password'=>'required' ]);
If you observe the above conditions Since the fields username and password have a required field. This says that the input fields username and password are mandatory. The username field has an additional [max:8] condition this means that the maximum number of characters allowed is 8. If any of the above conditions are violated the validation will fail.
Example
The following example shows the working of validating with the HTML form containing username and password.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class testvalidationController 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' ); } }
Inside resources/views/ create an HTML file with name "login.blade.php" as shown below
<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 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>
Add the following lines in routes/web.php.
routes/web.php use App\Http\Controllers\testvalidationController; Route::get('/validation',[testvalidationController::class, 'showform']); Route::post('/validation',[testvalidationController::class, 'validateform']);
Visit the following URL to test the validation.
http://localhost:8000/validation
Once you hit the URL and click on the Login button you should see the validation execution and since the username and password are mandatory fields the same is displayed to the user.
Displaying Error Messages
The validation errors are displayed inside the HTML by using the code given below. The $errors will have all the details about the errors and the same can be displayed to the user 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
Displaying custom Error Messages
You can add custom messages of your choice as shown below −
$this->validate($request,[ 'username'=>'required|max:8', 'password'=>'required', ], ['username.required' => 'The username field cannot be blank value.']);
Example
Following is an example demonstrating how display thecustom errors.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class testvalidationController extends Controller { public function showform() { return view('login'); } public function validateform(Request $request) { $this->validate($request,[ 'username'=>'required|max:8', 'password'=>'required', ], ['username.required' => 'The username field cannot be blank value.']); } }
Output
On executing the above program generates the following output
