While working with Laravel forms, you will face with this problem many time: how to pass errors and old values back to form and properly show them to the user?

 

OK so let's say we have input form for first and last name and we also added input error messages (if any)

                    <form id="settings-profile" method="post" action="{{route('put.user.profile.form')}}">

    @csrf

    <div class="mb-6">
        <label for="firstName" class="text-sm font-medium text-gray-900 block mb-2 dark:text-gray-300">{{__('frontend.First Name')}}</label>

        <input type="firstName" id="firstName" name="firstName" value="{{$profile->firstName}}" class="@if($errors->has('firstName'))" required>

        @if($errors->has('firstName'))
            <div class="error text-red-500 text-xs">{{ $errors->first('firstName') }}</div>
        @endif
    </div>

    <div class="mb-6">
        <label for="lastName" class="text-sm font-medium text-gray-900 block mb-2 dark:text-gray-300">{{__('frontend.Last Name')}}</label>

        <input type="lastName" id="lastName" name="lastName" value="{{$profile->lastName)}}" class="@if($errors->has('lastName'))" required>

        @if($errors->has('lastName'))
            <div class="error text-red-500 text-xs">{{ $errors->first('lastName') }}</div>
        @endif
    </div>
</form>
                  

 

and on backend side we have some validation logic:

                    if($validate->fails()) {
  return Redirect::back()->withErrors($validate);
}
                  

 

Ofcourse, when we redirect back we see the errors returned from validations but the values we have entered before form submit are gone!!!

OK, we have to figure out how to return old values together with error messages in Laravel.

Luckily,  Laravel has a pretty nice solution for this, when returning back from backend just add withInput()

                    if($validate->fails()) {
   return Redirect::back()->withInput()->withErrors($validate);
}
                  

 

hmmm but this is not working as it should, how can we display those old form input values  back in the form? ....again, Laravel's solution for this, add old('field_name') as a value to your input field and now your form will look like this:

                    <form id="settings-profile" method="post" action="{{route('put.user.profile.form')}}">

    @csrf

    <div class="mb-6">
        <label for="firstName" class="text-sm font-medium text-gray-900 block mb-2 dark:text-gray-300">{{__('frontend.First Name')}}</label>

        <input type="firstName" id="firstName" name="firstName" value="{{old('firstName')}}" required>

        @if($errors->has('firstName'))
            <div class="error text-red-500 text-xs">{{ $errors->first('firstName') }}</div>
        @endif
    </div>

    <div class="mb-6">
        <label for="lastName" class="text-sm font-medium text-gray-900 block mb-2 dark:text-gray-300">{{__('frontend.Last Name')}}</label>

        <input type="lastName" id="lastName" name="lastName" value="{{old('lastName'}}" required>

        @if($errors->has('lastName'))
            <div class="error text-red-500 text-xs">{{ $errors->first('lastName') }}</div>
        @endif
    </div>
</form>
                  

 

Nice, old values are now populated, everything looks like it should...but...wait a minute... if i just load the form, all previously submitted values are empty...damn!

 

So, how can i show old form values and default form values? 

The answer is, pass default value as a second param in old() method:

                    <input type="firstName" id="firstName" name="firstName" value="{{old('firstName', $profile->lastName)}}" required>
                  

 

And now our complete form with old values and default input values is ready:

                    <form id="settings-profile" method="post" action="{{route('put.user.profile.form')}}">

    @csrf

    <div class="mb-6">
    
        <label for="firstName" class="text-sm font-medium text-gray-900 block mb-2 dark:text-gray-300">{{__('frontend.First Name')}}</label>

        <input type="firstName" id="firstName" name="firstName" value="{{old('firstName', $profile->lastName)}}" required>

        @if($errors->has('firstName'))
            <div class="error text-red-500 text-xs">{{ $errors->first('firstName') }}</div>
        @endif

    </div>

    <div class="mb-6">
        
        <label for="lastName" class="text-sm font-medium text-gray-900 block mb-2 dark:text-gray-300">{{__('frontend.Last Name')}}</label>

        <input type="lastName" id="lastName" name="lastName" value="{{old('lastName'}}" required>

        @if($errors->has('lastName'))
            <div class="error text-red-500 text-xs">{{ $errors->first('lastName') }}</div>
        @endif

    </div>
</form>