Home  >  Q&A  >  body text

What's the way to keep a modal in Laravel from disappearing on submit?

I have a Laravel application that has a modal box on the view page that allows the user to add data to the database. By default, the modal disappears when the user clicks the submit button and the backend returns an error. I want to keep the modal box visible when an error occurs. How can I achieve this goal?

Button that triggers the modal box

<div><button class="add" data-modal-target="#modal-box">Add New Year</button></div>

My modal box

<div class="modal-box" id="modal-box">
    <div class="modal-header">
        <div class="title"><h1>Create School Year</h1></div>
        <div data-close-button class="modal-close-btn">
            <span class="material-symbols-sharp">close</span> 
        </div>   
    </div>
    <hr>
    <div class="modal-body">
        <form action="{{ route('create.year') }}" method="post">
            @csrf
            <div class="input-form">
                <div class="form-group">
                    <h3>School Year <span class="danger">*</span></h3>
                    <input type="text" name="name" id="name">
                    @error('name')
                        <p class="error danger">{{ $message }}</p>
                    @enderror    
                </div>
                <div>
                    <button type="submit" class="create">Create Year</button>
                </div> 
            </div>
        </form>    
    </div>
</div>

JavaScript switch modal box

const openModalButtons = document.querySelectorAll("[data-modal-target]");
const closeModalButtons = document.querySelectorAll("[data-close-button]");
const overlay = document.getElementById("overlay");

openModalButtons.forEach((button) => {
  button.addEventListener("click", () => {
    const modal = document.querySelector(button.dataset.modalTarget);
    openModal(modal);
  });
});

closeModalButtons.forEach((button) => {
  button.addEventListener("click", () => {
    const modal = button.closest(".modal-box");
    closeModal(modal);
  });
});

overlay.addEventListener("click", () => {
  const modals = document.querySelectorAll(".modal-box.active");
  modals.forEach((modal) => {
    closeModal(modal);
  });
});

function openModal(modal) {
  if (modal == null) return;
  modal.classList.add("active");
  overlay.classList.add("active");
}

function closeModal(modal) {
  if (modal == null) return;
  modal.classList.remove("active");
  overlay.classList.remove("active");
}

My Controller

public function CreateYear(Request $request)
{
    $validateData = $request->validate([
        'name' => 'required|unique:academic_years,name'
    ]);

    $data = new AcademicYear();
    $data->name = $request->name;
    $data->save();

    $notification = array(
        'message' => 'Academic Year Created Successfully!',
        'alert-type' => 'success'
    );

    return redirect()->route('view.year')->with($notification);
}

P粉755863750P粉755863750258 days ago461

reply all(1)I'll reply

  • P粉921165181

    P粉9211651812024-02-05 00:19:45

    In your form, you can add a hidden input with a schema ID. When submitting in the controller you will get the id and return it as a variable. When returning to the view, you check if the variable exists and if so, call the corresponding modal open :

    Somewhere in your view add:

    @isset($activeModal)
        sssccc
    @endisset

    reply
    0
  • Cancelreply