mercredi 25 décembre 2019

Laravel redirect with input and with errors not working

I am having issues with getting the ->withInput() and ->withErrors() to work so that upon a validation failure I can retrieve these in the blade.

This is what I have in the controller:

$rules = array(
   'mobile' => 'required'
);

$validator = Validator::make($request->all(), $rules);

if ($validator->fails()) {
    $fieldsWithErrorMessagesArray = $validator->messages();
    $fieldsWithErrorMessagesArray = $fieldsWithErrorMessagesArray->messages();

    $formattedErrorArray = array();
    foreach ($fieldsWithErrorMessagesArray as $key => $error) {
       $formattedErrorArray[$key] = $error[0];
    }

    return redirect()->back()->withInput()->withErrors($formattedErrorArray);
}

However in the blade when var_dump the $errors, I am getting this:

object(Illuminate\Support\ViewErrorBag)#267 (1) { ["bags":protected]=> array(0) { } }

I also tried this way :

$test = array(
            'mobile' => 'No jobs found. Please try searching with different criteria'
        );

return redirect()->back()->withInput()->withErrors($test);

This worked and I am not sure why the other one doesn't work. With this test array the blade file looks like this :

object(Illuminate\Support\ViewErrorBag)#264 (1) { ["bags":protected]=> array(1) { ["default"]=> object(Illuminate\Support\MessageBag)#265 (2) { ["messages":protected]=> array(2) { ["mobile"]=> array(1) { [0]=> string(59) "No jobs found. Please try searching with different criteria" }} ["format":protected]=> string(8) ":message" } } }


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/35WCE55
via IFTTT

Aucun commentaire:

Enregistrer un commentaire