Laravel 5.2验证错误未出现在Blade中

时间:2016-04-16 11:17:17

标签: php validation laravel

我想在视图页面中提供无效输入时显示验证错误。但错误消息未出现在视图页面中。如果可能的话请有人给出解决方案。

这是控制器:

public function saveStudent(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');
    }

以下是视图页面:

<html>
    <head> 
        <title> Create Course </title>

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

    <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> 
    </head>
    <body>

        <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"  placeholder="Name">
        </div>
        <div class="form-group">
          <label>Email</label>
          <input type="email" name="email" class="form-control"  placeholder="email">
        </div>
        <div class="form-group">
          <label>Phone Number</label>
          <input type="text" name="phone" class="form-control"  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>
    @if(count($errors)>0)
    <div class="alert alert-danger">
      <strong>Whoops!</strong> Enter Valid Input</br>
      <ul>
        @foreach($errors->all() as $error)
        <li>{{$error}}</li>
        @endforeach
      </ul>
    </div>
  @endif
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

修改您的功能,如下例所示:

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

         $validator = Validator::make($request->all(),$rules);
        if( $validator->fails() )
        {
           return redirect()->route( 'your route here') )->withErrors( $validator )->withInput();
        }else{

        $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');
       }
    }