在laravel5中没有显示任何验证错误消息


doesnot show any validation error messages in laravel5

我在控制器中使用了这个函数来存储带有验证的用户的记录。如果未满足验证要求,则不会存储数据,但不会显示任何验证错误消息。

public function store()
  {
        $input = Input::all();
        $validation = Validator::make($input, User::$rules);
        if ($validation->passes())
        {
            User::create($input);
            return Redirect::route('users.index');
        }
        return Redirect::route('users.create')
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
  }

我有型号:

<?php
namespace App;
class User extends BaseModel{
    protected $fillable = [
        'name', 'email', 'password', 'phone'
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
  public static $rules = array(
    'name' => 'required|min:5',
    'email' => 'required|email');
}

users.create视图文件:

@extends('layouts.user')
@section('main')
<h1>Create User</h1>
{{ Form::open(array('route' => 'users.store')) }}
    <ul>
        <li>
            {{ Form::label('name', 'Name:') }}
            {{ Form::text('name') }}
        </li>
        <li>
            {{ Form::label('username', 'Username:') }}
            {{ Form::text('username') }}
        </li>
        <li>
            {{ Form::label('password', 'Password:') }}
            {{ Form::password('password') }}
        </li>
        <li>
            {{ Form::label('password', 'Confirm Password:') }}
            {{ Form::password('password_confirmation') }}
        </li>        
        <li>
            {{ Form::label('email', 'Email:') }}
            {{ Form::text('email') }}
        </li>
        <li>
            {{ Form::label('phone', 'Phone:') }}
            {{ Form::text('phone') }}
        </li>

        <li>
            {{ Form::submit('Submit', array('class' => 'btn')) }}
        </li>
    </ul>
{{ Form::close() }}

@stop

取自Laravels文档

@if (count($errors) > 0)
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif
相关文章: