Retain Old Value on validation Error in Laravel

Laravel Retain Old value example; In this tutorial we will show you how to retain old form data on validation error in laravel. Mostly we need to retain the input textbox value, select box value, radio buttons value, checkbox value, textarea value.

In laravel Old value is when user add their details in form and mismatching some records then submit the form and validation is occurs then the input prefild data to retain itselp. Let’s see how to maintain old form data in laravel application.

Retain Old Value in Textbox Input

Retain old value for input textbox just like name, email etc in create form to store data, you can just update like below.

In Create Form

<div class="form-group">
  <strong>Name:</strong>
    <input type="text" name="name" value="{{ old('name') }}" class="form-control" placeholder="name">
</div>

In Edit Form

When you update form you can retain your input text-boxes values just like below code.

<div class="form-group">
  <strong>Name:</strong>
    <input type="text" name="name" value="{{ old('name', $user->name) }}" class="form-control" placeholder="name">
</div>

Retain Old Value in Radio Button

Retain old value for radio button in laravel just like below code in create form to store method’s.

In Create Form

<div class="form-group">
  <strong>Gender:</strong>
    <div class="custom-control custom-radio">
      <input type="radio" name="gender" value="male" @if(old('gender') == 'male') checked @endif class="custom-control-input">
        <label class="custom-control-label" for="customRadio1">Male</label>
     </div>
     <div class="custom-control custom-radio">
      <input type="radio" name="gender" value="female" @if(old('gender') == 'female') checked @endif class="custom-control-input">
       <label class="custom-control-label" for="customRadio2">Female</label>
  </div>
</div>

In Edit Form

When you update form you can retain your radio button’s values just like below code.

<div class="form-group">
  <strong>Gender:</strong>
    <div class="custom-control custom-radio">
      <input type="radio" name="gender" value="male" @if(old('gender') == 'male' || $user->gender == 'male') checked @endif class="custom-control-input">
        <label class="custom-control-label" for="customRadio1">Male</label>
     </div>
     <div class="custom-control custom-radio">
      <input type="radio" name="gender" value="female" @if(old('gender') == 'female' || $user->gender == 'female') checked @endif class="custom-control-input">
       <label class="custom-control-label" for="customRadio2">Female</label>
  </div>
</div>

Retain Old Value in Select Box

Retain old value for select box in laravel just like below code in create form to store method’s.

In Create Form

<div class="form-group">
  <strong>Country:</strong>
  <select name="country" class="form-control custom-select">
    <option selected>Select Country</option>
     @foreach($countries as $country)
      <option value="{{ $country['id'] }}" @if(old('country') == $country['id']) selected @endif>{{ $country['name'] }}</option>
     @endforeach
  </select>
</div>

In Edit Form

When you update form you can retain your select box values just like below code.

<div class="form-group">
  <strong>Country:</strong>
  <select name="country" class="form-control custom-select">
    <option selected>Select Country</option>
     @foreach($countries as $country)
      <option value="{{ $country['id'] }}" @if(old('country') == $country['id'] || $country['id'] == $user->country) selected @endif>{{ $country['name'] }}</option>
     @endforeach
  </select>
</div>

Retain Old Value in Textarea Field

Retain old value for textarea in laravel just like below code in create form to store method’s.

In Create Form

<div class="form-group">
   <strong>About:</strong>
   <textarea class="form-control" name="about" placeholder="About">{{ old('about') }}</textarea>
</div>

In Edit Form

When you are updating form you can retain your textare values just like below code.

<div class="form-group">
   <strong>About:</strong>
   <textarea class="form-control" name="about" placeholder="About">{{ old('about', $user->about) }}</textarea>
</div>

That’s all my friend’s the Retain input value, radio button, select box value, textarea values in laravel tutorial with example is over now.

1 thought on “Retain Old Value on validation Error in Laravel”

Leave a Comment