验证在 Laravel 5.5 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47778471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:05:38  来源:igfitidea点击:

Validation not working in laravel 5.5

laravelvalidationlaravel-5.5

提问by MMY

I installed a new Laravel 5.5 app and created a form in test.blade.phpview:

我安装了一个新的 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>

and created my PageController:

并创建了我的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');
   }
}

and in the web.phpadd my routes:

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

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

My problem is when I submit the form with or without data nothing happen and the page just reload and when I remove the validation code:

我的问题是,当我提交有或没有数据的表单时,什么也没有发生,页面只是重新加载,当我删除验证代码时:

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

it return the requests I can't understand what is the problem because I tested it before and it was working in Laravel 5.4. Can any one help me?

它返回的请求我不明白是什么问题,因为我之前测试过它并且它在 Laravel 5.4 中工作。谁能帮我?

采纳答案by Shiraz Coders

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

And your view code should be:

你的视图代码应该是:

    <!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>