1

I want to return back request data into fields if validation fails on create action. I've created resource controller and there is pre-set functions (index, create, store, show, edit, update, destroy).

In example, in the Edit controller I already have an ID of a record, then I can just select record from DB by it's ID and pass it's data to the view, so that I will able to see entry data in fields on page load:

<input name="name" value="{{ $name }}" />

public function edit($id)
{
   $item = Item::find($id);
   return view('pages.item.edit')->with($data);
}

But in the Create controller I do not have any ID of item to fetch and pass entry data for fields.

<input name="name" value="{{ old('name') }}" />

public function store(Request $request)
{
   $this->validate($request, [
      'name' => 'required|string|max:30'
   ])->withInput();
   return redirect('/items')->with('success', 'New item created!');
}

Should I use ->withInput() and where? {{ old('name') }} doesn't work too. This and this articles didn't help me

So, how to save fields if validation fails? For Edit method it will be also useful to save modified fields.

8
  • 1
    you can do it the hard way with $request->flash(); then retrieve it later after validation Commented Jun 21, 2018 at 17:58
  • 1
    You are doing something wrong, flashing to the session works by default, show us kernel.php and route you are using. Commented Jun 21, 2018 at 18:15
  • 1
    I personally never validate inside controller I just use FormRequests, its really good practise. Commented Jun 21, 2018 at 18:21
  • 1
    try this: laravel.com/docs/5.6/validation#manually-creating-validators Commented Jun 21, 2018 at 18:21
  • 1
    you can retreive the flashed input with session('field_name') Commented Jun 21, 2018 at 18:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.