What is the best practice to show old value when editing a form in Laravel?

陌路散爱 提交于 2019-12-03 01:39:57

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

I know this has already been answered but I thought i would leave a little snipet 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 statment 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, I hope it helps someone.

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

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">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!