我想在用户输入错误的同时在“视图”页面中显示验证错误。可以的是,当用户输入错误时,它不会在数据库中保存任何内容。但是用户视图页面中没有错误消息。如果有人发现错误,请帮助我。
这是控制器:
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'); }
这是路线:
Route::group(['middleware' => 'web'], function () { Route::get('/', function () { return view('index'); })->name('main'); Route::post('/saveUser',[ 'uses' => 'AppController@saveUser', 'as'=>'saveUser', ]); });
这是刀片视图页面:
@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
这是错误消息块:
@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
web如果您使用的是5.2.27或更高版本,请尝试删除中间件。现在,Laravel会自动将web中间件应用于内部的所有路由routes.php,如果您尝试手动添加它,则会出错。
web
routes.php
app/Providers/RouteServiceProvider.php的5.2.27版本现在将Web中间件添加到其中的所有路由routes.php:
app/Providers/RouteServiceProvider.php
protected function mapWebRoutes(Router $router) { $router->group([ 'namespace' => $this->namespace, 'middleware' => 'web', ], function ($router) { require app_path('Http/routes.php'); }); }