What is the best practice to show old value when editing a form in Laravel? [closed]
Asked Answered
T

6

61

I am writing the logic for an edit form and have some complications when displaying data in the inputs.

When I initially show the form, I show the records values like:

value="{{$dog->title}}"

Then when the form does not pass the validation I need to show the old input, so that user does not loose what he has already input. So I need to have a way to display old data like:

value="{{old('title')}}"

Because I need to input old data in case it exists, I ended up with this code:

value="{{$dog->title or old('title')}}"

And in controller I check if Request has old input, I assign the $dog var a null value.

I wanted to ask if that is considered an OK practice or is there a better and 'correct' way to do it?

Tannenberg answered 19/7, 2016 at 14:41 Comment(0)
F
157

Function old have default parameter if no old data found in session.

function old($key = null, $default = null)

You can replace expression in template with

value="{{old('title', $dog->title)}}"
Filament answered 2/2, 2017 at 13:1 Comment(2)
also sometime when you are accessing nested property and if that does not exist it will throw error to skip that i do something like this old('manager_id', $department->manager->id ?? '')Semiology
Why didn't I think to look this up earlier? This is gonna clear up so much bad code. Thank you, trulyAuk
S
32

I know this has already been answered but I thought I would leave a little snippet here for others in the future.

Setting old value on input as @ikurcubic posted can be used the same way on radio button or select:

<input type="text" name="name" value="{{ old('name', $DB->default-value) }}" />

Select option:

<option value="Jeff" {{ old('name', $DB->default-value) == 'Jeff' ? 'selected' : '' }}>Jeff</option>

Radio Button:

<input type="radio"  name="gender" value="M" {{ old('name', $DB->default-value)== "M" ? 'checked' : '' }} />

Another way of doing it; write a small if statement to determine which value should be evaluated:

@php                                  
if(old('name') !== null){
    $option = old('name'); 
}
else{ $option = $database->value; }
@endphp

<select name="name">
   <option value="Bob" {{ $option == 'Bob' ? 'selected' : '' }}>Bob</option>
   <option value="Jeff" {{ $option == 'Jeff' ? 'selected' : '' }}>Jeff</option>
</select>


<input type="radio"  name="gender" value="M" {{ $option == "M" ? 'checked' : '' }} />

<input type="radio"  name="gender" value="F" {{ $option == "F" ? 'checked' : '' }} />

Setting old value of input with an array name e.g name="name[]":

<input type="text" name="name[]" value="{{ old('name.0) }}" />

This will give you the old value of the input with an index of 0.

I have tested this and it works.

Sisyphus answered 23/5, 2018 at 9:2 Comment(2)
So, I tried the select option and it doesn't work. You're using selected like a PHP constant and that'd throw an exception. The way around it is to surround the selected with double quotes (making it a string)Corposant
thanks for this ..my select is working with this code, but not working wih check box, any help?Lieberman
I
11

Another way to do that is get the data from the dog class, like this:

value="{{old('title') ?? $dog->title }}"

Why? Because old() is for validation; when you fail validation, the input will remain available in the field. In the case where validation hasn't fired yet, value will be filled with $dog->title.

Insectarium answered 21/11, 2019 at 11:33 Comment(0)
P
4

I solved that issue in my application if you want to get old previous inserted value in input you should try this one I did like this.

function SaveData(Request $request)
{
       $sValidationRules = [
        'firstname' => 'required',
        'lastname' => 'required',
        'email' => 'required',
        'phone' => 'required',
      ];

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

      if ($validator->fails()) // on validator found any error 
      {
        // pass validator object in withErrors method & also withInput it should be null by default
         return redirect('Your Route Hare')->withErrors($validator)->withInput();
      }


      Employee::create($request->all());

      return redirect(' your route hare ');
}

& after then get old data in input field value like this using {{ Request::old('firstname') }} Happy Coding. :)

<input type="text" name="firstname" id="firstname"  value="{{ Request::old('firstname') }}" class="form-control" placeholder="First Name">
Papilla answered 2/10, 2019 at 11:17 Comment(0)
C
4

Old Data: input type text | phone | number | ...

 <input type="text" name="my_text" value="{{old('my_text')}}">

Old Data: input type checkbox | radio

 <input type="checkbox" {{ old('my_checkbox') == 'on' ? 'checked' : '' }} name="my_checkbox">
Camp answered 21/10, 2020 at 12:3 Comment(0)
T
1

There is nothing wrong with the way you are doing things as Laravel gives multiple ways to handle the situation you're describing.

What I would suggest is using the Laravel Collective Form and HTML packages to build your form. This package will automatically handle binding old request values to your form if validation fails

https://laravelcollective.com/docs/5.2/html

Tarnish answered 19/7, 2016 at 15:59 Comment(1)
Yeah, he's looking for Form Model Binding. That does exactly what he wants, and it saves some code writing :)Die

© 2022 - 2024 — McMap. All rights reserved.