Laravel re-show modal with same data when validation error

Viewed 265

I have Table showing data from database inside each row there is a button opening a modal with the current clicked row id like following:

<tbody>
      @foreach($receivedShipments as $shipment)
         <tr>
           <th class="text-center" scope="row">
            {{ $loop->iteration }}
            </th>
           <td class="font-w600 font-size-sm">
             <button type="button" class="btn btn-sm btn-light js-tooltip-enabled push"
             data-toggle="modal" data-target="#modal_handle_national_id" data-id="{{$shipment->id}}"
             data-original-title="{{trans('validation.attributes.deliver_to_receiver_customer')}}">                                             
              {{trans('validation.attributes.deliver_to_receiver_customer')}}</button>
           </td>
         </tr>
    @endforeach
</tbody>

the Modal modal_handle_national_id is :

   <div class="modal fade" id="modal_handle_national_id" >
     <div class="modal-content">
       <form class="js-validation" id="handleForm" action="" method="POST">
         @csrf
         @if(count($errors)>0)
          <div class="alert alert-danger ">
            <ul>@foreach($errors->all() as $error)
                <li>{{$error}}</li>
                @endforeach
            </ul>
          </div>
         @endif
          <input type="text" class=" col-xl-8" id="receiver_customer_nationalId"
            name="receiver_customer_nationalId"
           value="{{ old('receiver_customer_nationalId') }}">
          <button type="submit">Submit</button>
        </form>
      </div>
    </div>

i have script to hadle the action link of the form according to clicked button row id:

<script>
        @if (count($errors) > 0)
        $('#modal_handle_national_id').modal('show');
        @endif
        $('#modal_handle_national_id').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget);
            $('#handleForm').attr("action", "");
            var id = button.data('id');
            var url = '{{ route("shipments.handle", ":id") }}';
            url = url.replace(':id', id);
            $('#handleForm').attr("action", url);
        });
</script>

i need the modal to be re-showed with the same old clicked button id when there is a Laravel validation error
Any Help please?

1 Answers

You can use hidden.bs.modal event:

This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).

The snippet:

$('#modal_handle_national_id').on('hidden.bs.modal', function(e) {
    $(this).find('form').trigger('reset');
})
Related