Laravel 5.2 Validation Error not appearing in blade
Asked Answered
S

4

23

I want to show validation Error in View page while user giving wrong input. It's Ok that it's not saving anything in database while a user giving wrong input. But there is no error message in user view page. If anyone find the error, please help me out.

Here is the controller:

public function saveUser(Request $request){
        $this->validate($request,[
            'name' => 'required|max:120',
            'email' => 'required|email|unique:users',
            'phone' => 'required|min:11|numeric',
            'course_id'=>'required'
            ]);

        $user = new User();
        $user->name=  $request->Input(['name']);
        $user->email=  $request->Input(['email']);
        $user->phone=  $request->Input(['phone']);
        $user->date = date('Y-m-d');
        $user->completed_status = '0';
        $user->course_id=$request->Input(['course_id']);
        $user->save();
       return redirect('success');         
    }

Here is the route:

Route::group(['middleware' => 'web'], function () {

    Route::get('/', function () {
        return view('index');
    })->name('main');

      Route::post('/saveUser',[
        'uses' => 'AppController@saveUser',
        'as'=>'saveUser',
        ]);
    });

Here is the blade view page:

@extends('layouts.master')
@section('title')
Create User
@endsection
@section('content')
@include('partials.message-block')
        <div class="container" >

            <h3> Student Register </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" required placeholder="Name">
        </div>

        <div class="form-group">
          <label>Email</label>
          <input type="email" name="email" class="form-control" required placeholder="email">
        </div>

        <div class="form-group">
          <label>Phone Number</label>          
          <input type="text" name="phone" class="form-control" required placeholder="phone">
        </div>

        <div class="form-group">
            <label for="">Class</label>
            <select class="form-control input-sm" name="course_id" >
            @foreach($input as $row)
            <option value="{{$row->id}}">{{$row->name}}</option>
            @endforeach
            </select>
        </div>  

        <button type="submit" class="btn btn-default">Submit</button>
    {!! Form::close() !!}
        </div>

@endsection

And here is the error-message block:

@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
@if(Session::has('message'))
<div class="row">
    <div class="col-md-4 col-md--offset-4 success">
        {{Session::get('message')}}
    </div>
</div>

@endif
Selfrevealing answered 22/4, 2016 at 3:23 Comment(3)
Try to remove web middleware if you're using 5.2.27 or higher.Pacific
thanks for the help now it's working! :)Selfrevealing
Possible duplicate of ErrorBag is always empty in Laravel 5.2Wame
P
30

Try to remove web middleware if you're using 5.2.27 or higher. The thing is now Laravel automatically applies web middleware to all routes inside routes.php and if you're trying to add it manually you can get errors.

app/Providers/RouteServiceProvider.php of the 5.2.27 version now adds web middleware to all routes inside routes.php:

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'web',
    ], function ($router) {
        require app_path('Http/routes.php');
    });
}
Pacific answered 22/4, 2016 at 3:41 Comment(1)
Can you take a look on this question (#38826862)Coronet
D
1

Use Below line in your controller

use Validator;

Add below code in your controller function where your request is sent.

$validator  =   Validator::make($request->all(), [
     'fname'     =>  'required|max:20|min:4',
     'uemail'    =>  'required|email',
     'message'   =>  'required',
 ]);                                                                                                                                                                                                                               
if ($validator->fails()) {
       $messages = $validator->messages();
       return Redirect::back()->withErrors($messages)->withInput($request->all()); 
 }      

In your view page

 @if ($errors->any())
   <label for="fname" class="error">{{ $errors->first('fname') }}</label> 
 @endif

For display individual field wise error.

Dole answered 3/4, 2017 at 10:41 Comment(0)
J
1

if you are using laravel 5.2+ then please use the below code.

Moved \Illuminate\Session\Middleware\StartSession::class and \Illuminate\View\Middleware\ShareErrorsFromSession::class from web $middlewareGroups to $middleware in app\Http\Kernel.php

Jacklighter answered 17/11, 2020 at 10:11 Comment(0)
L
0

Alexey is correct and if you're not comfortable with that then you can just add this code into your view for the session messages to show.

@if(Session::has('message'))
            <div class="alert alert-success">{{Session::get('message')}}</div>
        @endif
        @if(count($errors)>0)

            <ul>
                @foreach($errors->all() as $error)
                <li class="alert alert-danger">{{$error}}</li>
                @endforeach
            </ul>
        @endif

I've done this in laravel 5.5. Please do confirm if this helps you out.

Laurynlausanne answered 6/4, 2018 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.