Laravel 5.1表单请求验证返回输入

时间:2015-10-21 05:56:36

标签: laravel laravel-5 laravel-5.1

我想知道是否有人知道我该怎么做。

目前,我正在使用表单请求验证,因此我的store方法看起来像

public function store(ProfileStore $request)
{
// do stuff. 
   Input::flush(); 
   return redirect()->back();
} 

^注意输入刷新,我不希望某些输入存储为"旧输入"或者传回表格,所以我正在冲洗它。

然后在我的ProfileStore中我有一些基本的验证(例如。

public function rules()
{
    return [
        'title' => 'required|max:255',
        'body' => 'required',
    ];
}

问题是当我使用请求验证时,它将输入传递回表单以及错误消息。我试过从验证文件中刷新输入,但不起作用。

如果我从store方法手动创建验证器而不使用请求验证,它可以正常工作并且不会传回输入。

更新:

所以我正在使用Laravel Collective Forms& HTML,我认为它与此有关。这很奇怪,因为我使用Form :: open,据我所知只有Form模型绑定应该在Form :: model上执行此操作。

如果我删除

Form::text('testfield', null);

并替换为标准输入

<input tpye="text" name="testfield" value="">

验证后没有输入正确的输入。但是,当使用Form :: input值时,将从验证中返回。

      {!! Form::open(['route' => ['frontend.account.profile.save'], 'id' => 'profile-update', 'class' => 'profile', 'role' => 'form']) !!}
        <div class="form-body">
            <div class="row" id="billing_details_div">
                <div class="col-xs-12">

                    {{-- Input gets passed back after failed validation when using Form::text() --}}

                     Form::text('testfield', null);

{{-- No Input gets passed back when using stantard HTML input
    <input tpye="text" name="testfield" value=""> --}} 

               </div>
                <div class="col-md-12 text-center">
                    <button type="submit" class="btn btn-lg btn-success"><span class="glyphicon glyphicon-floppy-disk"></span> Update Profile</button>
                </div>
            </div>
        </div>
        {!! Form::close() !!}

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

ProfileStore请求类中编写此函数,它应该解决这个问题。

public function response(array $errors)
{
    if ($this->ajax() || $this->wantsJson()) {
        return new JsonResponse($errors, 422);
    }

    return $this->redirector->to($this->getRedirectUrl())
                                 ->withErrors($errors, $this->errorBag);
}

我不太确定它是否有效,所以如果有,请告诉我。 ;)

<强>更新

试试这个

protected $dontFlash = ['password', 'password_confirmation', 'your-other-inputs'];
相关问题