Laravel 5.2 form validation not showing errors? [duplicate]

北战南征 提交于 2019-12-14 02:54:22

问题


My form validation is not showing any error

My routes.php file is as follows:

Route::post('/user/add/form', [
    'uses' => 'AdminController@adduser',
    'as' => 'admin.add.user.to.database' 
    ]); 

My adduser function in AdminController contains following:

$this->validate($request,[
        'email'=> 'required|email',
        'name'=> 'required',
        'password'=> 'required',
        'aright'=> 'required',
        'dob' => 'date',
        'publication'=> 'string',
        'utype' => 'required'
]);

This is the blade view which I am using for form. This asks for some of the fields whihc are shown in controller function and should show the error if the required field are not filled but it is not showing anything.

@extends('layouts.admin-master')

@section('styles')
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap /3.3.6/css/bootstrap.min.css">
@endsection

@section('scripts')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<script type="text/javascript">
    var token = "{{ Session::token() }}";
</script>
@endsection
@section('content')
@include('includes.info')
@if(count($errors) > 0)


        @foreach($errors->all() as $error)
            <h1> {!! $error->first() !!} </h1>
        @endforeach

@endif
<div class="container">
    <h2>Add User Form</h2>
<form  class="form-horizontal" method="post" action="{{ route('admin.add.user.to.database') }}">
    <div class="row">

        <div class="col-sm-6">
            <div class="form-group">
              <label class="control-label col-sm-2" for="email">Email:</label>
              <div class="col-sm-10">
                <input type="email" class="form-control" id="email" placeholder="Enter email" name="email" value="{{ Request::old('email') }}">
              </div>
            </div>
        </div>

        <div class="col-sm-6">
            <div class="form-group">
              <label class="control-label col-sm-2" for="name">Name:</label>
              <div class="col-sm-10">          
                <input type="text" class="form-control" id="name" placeholder="Enter name" name="name">
              </div>
            </div>
        </div>

    </div>

    <div class="row">
        <div class="col-sm-6">    
            <div class="form-group">
              <label class="control-label col-sm-2" for="pwd">Password:</label>
              <div class="col-sm-10">          
                <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="password">
              </div>
            </div>
        </div>

        <div class="col-sm-6">
            <div class="form-group"> 
                <label class="control-label col-sm-2"  for="aright">Access Type:</label>
                    <select class="col-sm-10" id="aright" name="aright">
                        <option value="2">Normal Member</option>
                        <option value="1">Admin</option>
                    </select>
            </div>
        </div>    
    </div>

    <div class="row">

        <div class="col-sm-6">
            <div class="form-group">
              <label class="control-label col-sm-2" for="publication">Publication:</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="publication" placeholder="Enter publication link" name="publication">
              </div>
            </div>
        </div>

        <div class="col-sm-6">
            <div class="form-group">
              <label class="control-label col-sm-2" for="dob">Date of birth:</label>
              <div class="col-sm-10">          
                <input type="date" class="form-control" id="dob" placeholder="Enter DOB" name="dob">
              </div>
            </div>
        </div>

    </div>
    <div class="form-group"> 
                <label class="control-label col-sm-2"  for="utype">User Type:</label>
                    <select class="col-sm-10" id="aright" name="utype">
                        <option value="2">Faculty</option>
                        <option value="1">Student</option>
                    </select>
    </div>
    <div class="form-group text-center">        
      <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-primary">Submit</button>
      </div>
    </div>
     <input type="hidden" name="_token" value="{{ Session::token() }}">
</form>

</div>

@endsection

回答1:


As of v5.2.27, released on 2015-03-25, all routes in app\Http\routes.php are now in the web middleware group by default. If you have explicitly specified this middleware group inside your app\Http\routes.php file, you need to remove it.




回答2:


I guess the issue is that you are calling first() on the error? first() is a method on the MessageBag instance ie $errors. See the problem line

<h1> {!! $error->first() !!} </h1>

Change to

<h1> {{ $error }} </h1>


来源:https://stackoverflow.com/questions/36483719/laravel-5-2-form-validation-not-showing-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!