验证不适用于laravel 5.5

时间:2017-12-12 17:39:15

标签: laravel validation laravel-5.5

我安装了一个新的Laravel 5.5应用,并在test.blade.php视图中创建了一个表单:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
        <form method="POST" action={{ route('contact') }}>
            {{ csrf_field() }}
            <input type="text" name="title">
            <input type="text" name="body">
            <input type="submit"  value="click">
        </form>
</body>
</html>

并创建了我的PageController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Pagecontroller extends Controller
{
   public function index(Request $request){
            $this->validate($request,[
                'title' => 'required',
                'body' => 'required',
            ]);

            return View('View');
   }
}

并在web.php添加我的路线:

<?php
Route::get('/', function () {
    return view('welcome');
});
Route::get('/test',function(){
    return View('test');
});
Route::post('/contact',['uses'=>'PageController@index','as'=>'contact']);

我的问题是,当我提交带有或没有数据的表单时,没有任何事情发生,页面只是重新加载,当我删除验证码时:

 $this->validate($request,[
                    'title' => 'required',
                    'body' => 'required',
                ]);

它返回请求我无法理解是什么问题因为我之前测试了它并且它在Laravel 5.4中工作。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    class Pagecontroller extends Controller
    {
       public function index(Request $request){
        $request->validate([
            'title' => 'required',
            'body' => 'required',
                ]);

                return View('View');
               }
            }

您的观看代码应为:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    @if ($errors->any())
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
    @endif
            <form method="POST" action={{ route('contact') }}>
                {{ csrf_field() }}
                <input type="text" name="title">
                <input type="text" name="body">
                <input type="submit"  value="click">
    </form>
    </body>
    </html>