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

时间:2016-05-11 13:15:57

标签: php validation laravel request

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

这是我的代码:

应用/ HTTP / routes.php文件

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

应用/ HTTP /请求/ 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',
        ];
    }
}

应用/ HTTP /控制器/ 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());
    }
}

,这是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() !!}

供参考,表示输出

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

这是我在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 |
+--------+----------+-------------------------+------+-----------------------------------------------------------------+--------------+

7 个答案:

答案 0 :(得分:3)

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

试一试: 在app \ Http \ Kernel.php中将\Illuminate\Session\Middleware\StartSession::classweb $middlewareGroups移至$middleware

答案 1 :(得分:2)

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

接受:application / json

进入“标题”标签,否则您将不会收到应用程序错误,并且页面会刷新并返回200

答案 2 :(得分:0)

您是否安装了表单生成器? Laravel Collective已经就如何安装HTML&amp;表格外墙。您可以在此处找到它:https://laravelcollective.com/docs/5.2/html

答案 3 :(得分:0)

不确定它究竟是什么,但这里有几件事要尝试:

明确添加method => post

{!! Form::open(array('url' => 'update_name', 'method' => 'post')) !!}

检查您的路线以确保路线已正确注册。运行:

`php artisan route:list`

如果所有其他方法都失败,请尝试使用Postman手动发送POST请求。这至少可以让您确定问题是在您的表单中还是在设置POST路由时。

最后,与此问题无关,我建议保持简单。在这样的路线上,不需要有一个Request类,只需使用内联验证。

public function updateName(Illuminate\Http\Request $request) {
    $this->validate($request, [
        'first_name' => 'required',
        'last_name' => 'required'
    ]);
    return dd($request->all());
}

答案 4 :(得分:0)

您正在获取TokenMismatchException ..请确保您的会话存储路径是可写的

答案 5 :(得分:0)

我解决了我改变了这个问题:

SUMIF

为:

IF

谢谢你们:)

答案 6 :(得分:-1)

如果您要验证的字段数大于提交的表单上的数量,您也会收到此错误。

确保您没有验证提交表单上存在的更多字段。