Laravel 5.2表单验证没有显示错误?

时间:2016-04-07 17:52:59

标签: php laravel laravel-5.2

我的表单验证未显示任何错误

我的routes.php文件如下:

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

我在AdminController中的adduser函数包含以下内容:

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

这是我用于表单的刀片视图。这会询问控制器功能中显示的某些字段,如果未填充所需字段但未显示任何内容,则应显示错误。

@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

2 个答案:

答案 0 :(得分:2)

从2015年3月25日发布的v5.2.27开始,app \ Http \ routes.php中的所有路由默认情况下都在Web中间件组中。如果您已在app \ Http \ routes.php文件中明确指定了此中间件组,则需要将其删除。

答案 1 :(得分:0)

我想问题是你在错误上调用first()? first()是MessageBag实例上的一个方法,即$ errors。请参阅问题专栏

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

更改为

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