Monday, June 27, 2016

How to show Validation Error in Laravel? | Request Validation Error in Laravel.


laravel validation


In this session how you can show validation error from Server Side to Client Side in Laravel Application. It's quite easy just follow the following steps.

1. Let's  Make  routes first. Suppose we want to save user information. So we need a user  getUser page where we gonna save user details.

routes.php

Route::group(['middleware' => ['web']], function () {
    Route::get('/getUser',[
'uses'=>'UserController@getUser',
'as'=>'getUser'
]);
Route::post('/saveUser',[
'uses'=>'UserController@saveUser',
'as'=>'saveUser'
]);
});

2. Now Let's Make UserController on which we gonna write the logic of HTTP request.

UserController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Response;
class UserController extends Controller
{
    public function getUser()
    {
     return view('getUser');
    }
    public function saveUser(Request $request)
     {
         $this->validate($request,[
            'name' => 'required',          
            'email' => 'required|unique:users',          
            ]);
     $user=new User();
     $user->name= $request->Input(['name']);
     $user->email=$request->Input(['email']);
     $user->save();
     return redirect('getUser');
     }
}

3. Now thefinal Let's make the view page where we will store the data and  show the Error.

getUser.blade.php



<html>
<head>
<title> Create User</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
 integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" 
 crossorigin="anonymous">
</head>
<body>
/* Don't forget to add this Code inside of your view page, because this is where we gonna show the error message */

@if(count($errors) > 0)
    <div class="row">
        <div class="col-md-4 col-md-offset-4 error">
            <ul>
                @foreach($errors->all() as $error)
                    <li>{{$error}}</li>
                @endforeach
            </ul>
        </div>
    </div>
@endif
<div class="container" >
<h3> Create Department </h3>
{!! Form::open(array('route' => 'saveUser','class'=>'form-horizontal','method'=>'POST'))  !!}
    {!! Form::token(); !!}
    {!!   csrf_field() ; !!} 
        
   <div class="form-group">
     <label>Name</label>
     <input type="text" name="name" class="form-control"  placeholder="Name">
   </div>
      
      <div class="form-group">
     <label>Email</label>
     <input type="email" name="email" class="form-control"  placeholder="Email">
   </div>
  
   <button type="submit" class="btn btn-default">Submit</button>
{!! Form::close() !!}
</div>
     
</body>
</html>

4. Now if we go to the required route and do the HTTP request (here we gonna store user data in database), let's not put any data in form and try to save in database and you'll see the Error Message appearing on the page like following :






error validation demo


5.  So, as you see it's very easy to show validation Error in your web page. You can show flash messages also in your web page. I'm gonna make a tutorial on flash Error Message very soon. So till then stay connected with my blog.

Thank you.

























Related Articles

0 comments:

Post a Comment