1

Currently, I am trying to use Form Request and seperate the logic to validate out of controller. The down side is that the documentation is unclear on how I should pass states back to the controller when the validation instance fails and I am redirected back. I am trying to temporarily store an image inside the server, so the saved image would be displayed back to the user when they fail the validations.

Here is a part of my controller:

protected function session_data(Request $request, $id = null)
{
    // process $data
    return $data;
}


//view model of edit page
public function edit(Request $request, $id)
{
    if ($request->session()->has('_old_input')) {
        $data = $this->session_data($request, $id);
    } else { // get session data if exists, or search the db.
        $data = Seminar::index_list($id)[0];
    }
        
    return view('exhibitor.seminar.edit')
           ->with('entry', $data);
}


// logic to handle post request from edit page
public function update(SeminarEditRequest $request) //validate post request
{
    return 'stub';
}

A user will make a put request to update. If the validation fails, it will redirect back towards the edit method.

By default, the implementation of _old_input session key which contains information about previous form data wouldn't contain information about the uploaded file when a user makes a post/put request to update method and fails the rules defined in SeminarEditRequest. My question is how can I pass (and store) information about uploaded files inside SeminarEditRequest.php?

Silver Flash
  • 871
  • 3
  • 7
  • 16
  • Does this answer your question? [Laravel 5.1: keep uploaded file as old input](https://stackoverflow.com/questions/33158328/laravel-5-1-keep-uploaded-file-as-old-input) – Remul Feb 10 '21 at 08:45
  • @Remul The top rated answer does not answer how I should implement it. Neither does it state a method to pass data directly to the controller. (doesn't have to be files). Currently, I have an ongoing implementation somewhat similar to what the best answer suggests. – Silver Flash Feb 10 '21 at 09:14

1 Answers1

0

It's an old question but I want to post an answer. We can use session flash when we want to send data from FormRequest to Controller. For example;


use Illuminate\Contracts\Validation\Validator;

class EditRequest extends FormRequest
{

    // ...

    protected function failedValidation(Validator $validator)
    {
        $transferObject = (object)[
            'test' => true
        ];

        $this->session()->flash('transfer_data_key', $transferObject);
    }

}
class TestController extends Controller
{
    public function edit(EditRequest $req)
    {
        dd($req->session()->get('transfer_data_key'));
    }
}
ahmeti
  • 464
  • 5
  • 12