limit
limit

Reputation: 647

Laravel 5.1 Form Request Validation Returns input

I am wondering if anyone knows how I can do this.

Currently, I am using form request validation so my store method looks something like

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

^ Note the input flush, I don't want certain input stored as "old input" or passed back to the form so I am flushing it.

and then in my ProfileStore I have a some basic validation (eg.

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

The problem is when I use Request Validation, its passing the the input back into the form along with the error messages. I have tried flushing input from the validation file, but doesn't work.

If I manually create a validator from my store method and not use Request Validation it works fine and will not pass back input.

Update:

So I am using Laravel Collective Forms & HTML, I think its related to that. Which is weird because I am using Form::open and as far as I know only Form model binding should be doing this on Form::model..

If I remove

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

and replace with standard input

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

No input is returned after validation which is correct. However when using Form::input values are returned from validation.

      {!! 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() !!}

Any ideas?

Upvotes: 0

Views: 1143

Answers (1)

Ali Nazari
Ali Nazari

Reputation: 1438

Write this function in your ProfileStore request class and it should fix that.

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

I'm not quite sure if it works, so let me know if it does. ;)

Update

Try this as well

protected $dontFlash = ['password', 'password_confirmation', 'your-other-inputs'];

Upvotes: 2

Related Questions