Nikhil Shukla
Nikhil Shukla

Reputation: 31

How to Retain same value entered in input fields after laravel validation fails

My Blade File Code :-

    <div class="col-md-6">
                          <div class="form-group">
                             <label for="city">Assigned Hour<span class="text-danger">*</span></label>
                             <div class="dateBox">
                                <input type="number" name="assigned_hour[[]" class="form-control newAssignedHour" autocomplete="off" value="" required>
                             </div>
                          </div>
                       </div>
                       <div class="col-md-6">
                          <div class="form-group">
                             <label for="city">Consumed Hour<span class="text-danger">*</span></label>
                             <div class="dateBox">
                                <input type="number" name="consumed_hours[[]" class="form-control newConsumedHour" autocomplete="off" value="" required>
                             </div>
                          </div>
                       </div>
                       <div class="col-md-6">
                          <div class="form-group c-label datepicker-wrap">
                             <label for="city">Date<span class="text-danger">*</span></label>
                             <div class="dateBox">
                                <input type="text" name="date[]" class="form-control input-daterange newDate" id="dateInput" autocomplete="off" value="" required>
                                <span href="" class="dateCalender emp-cost-i">
                                <i class="fa fa-calendar"></i> </span>
                             </div>
                          </div>
                       </div>

my Controller :-

 $valid = request()->validate([
        'project_id' => 'required|int|exists:projects,project_id',
        'email_id' => 'required|email',
        'emp_id' => 'required|int|exists:contacts,employee_id',
        'project_name' => 'required|string|exists:projects,name',
        'GMWO' => 'required',
        'employee_name' => 'required|string',
        'newJobIds' => 'required|array',
        'newJobNames' => 'required|array',
        'newAssignedHours' => 'required|array',
        'newConsumedHours' => 'required|array',
        'newDates' => 'required|array'
    ]);

Can Anyone Suggest How to Retain the old Value for these fields. {{ old('field_name') }} that doesn't work for this. thanks in Advance

Upvotes: 0

Views: 1069

Answers (1)

Sanjay Prajapati
Sanjay Prajapati

Reputation: 29

Please try with the below method when validation gets failed and is redirected to form in the controller

->withInput()

For more reference see the below link https://laravel.com/docs/9.x/responses#redirecting-with-input

Upvotes: 1

Related Questions