Laravel 5.2 表单验证请求无法正常工作

emu*_*tol 5 php validation request laravel

当我点击提交按钮时,没有任何反应,只是刷新页面。

这是我的代码:

应用程序/Http/routes.php

Route::group(['middleware' => ['web']], function () {
    Route::get('profile/edit', 'UserController@editProfile');
    Route::post('update_name', 'UserController@updateName');
});
Run Code Online (Sandbox Code Playgroud)

应用程序/Http/Request/UpdateNameRequest.php

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use Illuminate\Support\Facades\Auth;

class UpdateNameRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'first_name' => 'required|min:2|alpha',
            'last_name' => 'required|min:2|alpha',
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\User;

class UserController extends Controller
{
    public function __construct() {
        $this->middleware('auth');
    }

    public function editProfile() {
        if (Auth::user()->role_id === 3) {
            return view('profile.crew.edit');          
        }
    }

    public function updateName(Requests\UpdateNameRequest $request) {
        return dd($request->all());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 html 表单

{!! Form::open(array('url' => 'update_name')) !!}
<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
    <label class="control-label">First Name</label>
    <input type="text" class="form-control" name="first_name" value="{{ old('first_name') }}" placeholder="{{ Auth::user()->first_name }}">

    @if ($errors->has('first_name'))
        <span class="help-block">
            <strong>{{ $errors->first('first_name') }}</strong>
        </span>
    @endif
</div>
<div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
    <label class="control-label">Last Name</label>
    <input type="text" class="form-control" name="last_name" value="{{ old('last_name') }}" placeholder="{{ Auth::user()->last_name }}">

    @if ($errors->has('last_name'))
        <span class="help-block">
            <strong>{{ $errors->first('last_name') }}</strong>
        </span>
    @endif
</div>
<button type="submit" class="btn btn-success">Update name</button>
{!! Form::close() !!}
Run Code Online (Sandbox Code Playgroud)

供参考这里的表单输出

<form method="POST" action="http://localhost:8000/update_name" accept-charset="UTF-8">
    <input name="_token" type="hidden" value="VViupfPaPCQCk5aeUdc27Pt2Z8J7Hx1Y2khC0IY9">
    <div class="form-group">
        <label class="control-label">First Name</label>
        <input type="text" class="form-control" name="first_name" value="" placeholder="Hans">

    </div>
    <div class="form-group">
        <label class="control-label">Last Name</label>
        <input type="text" class="form-control" name="last_name" value="" placeholder="Padberg">

    </div>
    <button type="submit" class="btn btn-success">Update name</button>
</form>
Run Code Online (Sandbox Code Playgroud)

这是我在 php artisan route:list 中的输出

+--------+----------+-------------------------+------+-----------------------------------------------------------------+--------------+
| Domain | Method   | URI                     | Name | Action                                                          | Middleware   |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+--------------+
|        | GET|HEAD | /                       |      | Closure                                                         | web          |
|        | GET|HEAD | home                    |      | App\Http\Controllers\HomeController@index                       | web,web,auth |
|        | GET|HEAD | login                   |      | App\Http\Controllers\Auth\AuthController@showLoginForm          | web,guest    |
|        | POST     | login                   |      | App\Http\Controllers\Auth\AuthController@login                  | web,guest    |
|        | GET|HEAD | logout                  |      | App\Http\Controllers\Auth\AuthController@logout                 | web          |
|        | POST     | password/email          |      | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,guest    |
|        | POST     | password/reset          |      | App\Http\Controllers\Auth\PasswordController@reset              | web,guest    |
|        | GET|HEAD | password/reset/{token?} |      | App\Http\Controllers\Auth\PasswordController@showResetForm      | web,guest    |
|        | GET|HEAD | profile                 |      | App\Http\Controllers\UserController@getProfile                  | web,web,auth |
|        | GET|HEAD | profile/edit            |      | App\Http\Controllers\UserController@editProfile                 | web,web,auth |
|        | GET|HEAD | register                |      | App\Http\Controllers\Auth\AuthController@showRegistrationForm   | web,guest    |
|        | POST     | register                |      | App\Http\Controllers\Auth\AuthController@register               | web,guest    |
|        | POST     | update_email            |      | App\Http\Controllers\UserController@updateEmail                 | web,web,auth |
|        | POST     | update_name             |      | App\Http\Controllers\UserController@updateName                  | web,web,auth |
|        | POST     | update_password         |      | App\Http\Controllers\UserController@updatePassword              | web,web,auth |
|        | POST     | update_profile_picture  |      | App\Http\Controllers\UserController@updateProfilePicture        | web,web,auth |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+--------------+
Run Code Online (Sandbox Code Playgroud)

Mhm*_*hmd 10

我只想为那些使用邮递员或此类别中的任何其他应用程序检查验证的人添加一些内容,请确保添加

接受:应用程序/json

到标题选项卡,否则您不会收到应用程序错误,并且页面只是刷新并返回 200


Md *_*har 4

您的错误变量返回空值。这就是为什么没有显示错误,只是看起来页面正在刷新。您的验证正在运行,但错误值未显示。

尝试一下:在 app\Http\Kernel.php 中\Illuminate\Session\Middleware\StartSession::classweb $middlewareGroups移动到$middleware