What is the best way to display flash messages within a Bootstrap modal dialog box?

I am facing an issue with my bootstrap modal window and form. I use flask.flash() to display error messages, but when I click upload for the first time, the modal window closes. The error message only shows up when I reopen the modal window. I want the error message to appear when I click the Upload button without closing the modal if there is an error. If there are no errors, it should work as usual. Is this something that can be fixed by using JavaScript (which I am not familiar with), or can it be achieved through flask and bootstrap? Any help would be appreciated.

https://i.sstatic.net/XQYFa.png https://i.sstatic.net/XaU8E.png

<form method="post" action="{{ url_for('index') }}" enctype="multipart/form-data">
                <div class="modal-body">
                    {% with messages = get_flashed_messages(with_categories=true) %}
                        {% if messages %}
                            {% for category, message in messages %}
                            <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                               {{ message }}
                              <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                              </button>
                            </div>
                            {% endfor %}
                        {% endif %}
                    {% endwith %}

                    <div class="input-group mb-3">
                      <div class="custom-file">
                        <input type=file name=dump_file class="file-input" id="custom-file-input" multiple>
                        <label class="custom-file-label" for="custom-file-input" data-browse="Browse">Choose file</label>
                      </div>
                    </div>
                    <div class="custom-control custom-radio custom-control-inline">
                      <input type="radio" id="customRadioInline1" name="radio" class="custom-control-input" value="linux">
                      <label class="custom-control-label" for="customRadioInline1">Linux</label>
                    </div>
                    <div class="custom-control custom-radio custom-control-inline">
                      <input type="radio" id="customRadioInline2" name="radio" class="custom-control-input" value="windows">
                      <label class="custom-control-label" for="customRadioInline2">Windows</label>
                    </div>
                    <!-- <span class="glyphicon glyphicon-paperclip"></span> -->
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-info">Upload</button>
                </div>
          </form>
      </div>
    </div>
  </div>

Answer №1

Yes, I also faced this issue before and found a simple solution using JQuery: This script will display the modal only if there is an error (flash warning present)

where .flash refers to the specific div you assigned for your flash warning

The modal ID in this case is changePasswordModal

<script>
    $(document).ready(function() { // Executes when page is fully loaded
  if ( $('.flash').length ) { // Check if any DOM element has the class of 'has-error'
     $('#changePasswordModal').modal('show'); // Show the Modal
  }
});
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS: Customize form elements based on model type

I need to work with an Angular model that contains ConfigValues. This is essentially a Dictionary object passed from C# which looks something like this: { Name: "Config Name", Value "True", Type: 0 // boolean } Some of these values are boolean, ...

Creating an HTML drop-down menu for selecting a vehicle

I am looking to incorporate two drop down menus on my website. One for selecting car brands and the other for choosing specific car models. I have successfully set up the drop down menu for car brands, but it currently only displays four options: Maruti Sw ...

PHP does not receive the uploaded image

I have encountered an issue while trying to create a form for editing a product. Whenever I upload an image, my PHP code is unable to locate the image and throws the following error: Notice: Undefined index: image in C:\xampp\htdocs\pages&bs ...

Is it possible to reload the page using Node.js and Express?

In my small Node and Express application, I am looking to automatically refresh the page when a user connects. I attempted using res.redirect('index.html') in a particular section of code, but encountered an error when trying to connect to the ro ...

How can I update the image source using Angular?

<div class="float-right"> <span class="language dashboard" data-toggle="dropdown"> <img class="current" src="us-flag.png" /> </span> <div class="dropdown dashboar ...

ng-repeat refreshes after compilation

I have a collection of upcoming events retrieved through a JSON request to my server. These events are displayed using ng-repeat in my application. I am looking to add functionality where users can like or unlike an event by clicking on a button. <a ng ...

Here's a guide on executing both GET and POST requests using a single form

Currently, I am developing a web application which involves using a GET request to display checkboxes in a form. The selected data from the checkboxes needs to be sent back to the server using a POST request. However, I'm facing an issue with performi ...

Loading STL files can sometimes lead to issues when accessing the world matrix incorrectly

While working on a three.js project, I encountered some issues with loading vertices from an STL file and converting them to world coordinates. It seems like the matrix application isn't working properly, and I suspect it could be related to the loadi ...

What is the best way to send a function along with personalized data?

Currently, I am working on a project using node.js with socket.io. I am looking for a way to have socket.on() use a unique callback function for each client that joins the server. Here is my current technique: I have created a simple JavaScript class whi ...

Calculating the mean value of the numbers provided in the input

I'm struggling with calculating the average of numbers entered through a prompt window. I want to display the numbers as I've done so far, but combining them to find the average is proving difficult. Here's the code I have: <html> &l ...

Looking to retrieve the dynamic "Publish Date" value from a webpage using HtmlUnit in Java?

As part of a coding exercise, I am currently working on a project that involves comparing the current system date with dates on various web pages to determine if there are any new updates. While most of the pages work as expected, there is one particular p ...

Is there a way to obtain the current time upon clicking the timepicker?

Here is a snippet of my code: // TIME $('#timepicker').datetimepicker({ minDate: minTime, }) .on('dp.show', function(e){ console.log(selectedDate); if($(this).val()==""){ $(this).val(minTime.format("HH:mm") ...

"Learn how to add up elements in an array based on their unique IDs and generate a new array using

There is an array called data that looks like this: const data = [ {"id": "One", "number": 100}, {"id": "One", "number": 150}, {"id": "One", "number": 200}, ...

In JavaScript, the gallery feature with Lightbox effect creates a unique touch as only half of the screen fades to black in

Hello everyone, I'm a complete beginner when it comes to programming so please be gentle with me on my first question :) I'm trying to put together a simple website with a lightbox gallery, and you can check out what I have so far here. The prob ...

Node.js reads and writes a JSON file, encountering issues with another application in the process

I'm currently facing an issue with my node.js server (express.js) and a json file used for reading and writing data through a simple API. Interestingly, when the node.js server is stopped, another application can add data to the json file without any ...

What is the best way to conceal a callback form once it has been successfully submitted?

In my callback form, everything seems to be functioning properly. However, there is a slight issue when the client hits the submit button multiple times. Each time they do so, a notification saying "Your request sent successfully" pops up next to the butto ...

Is there a way to position the image at the exact center of the column?

Currently, I am attempting to design a container that includes an image on the left, a title and description in the center, and a button on the right. However, I am facing difficulties centering the image in the first column and the button in the last colu ...

Utilizing columns within the 'segment' element in Semantic-ui to enhance layout design

I am looking to divide the ui-segment into 3 columns and display the ui-statistics evenly. Below is the code I have used in an attempt to achieve this: <div class="ui container"> <div class="ui segment"> <h3 class="ui header"> ...

What is the process for integrating a JOI validator with a Firebase function?

Is there a way to incorporate JOI validator into Firebase functions as middleware? When calling a Firebase function, it looks something like this: exports.createUserAccount = region("europe-east1").https.onCall(createAccount); // createAccount is ...

Guide on effectively exporting an NPM module for all browsers:

I have developed an NPM module which looks like this: class MyModule { // code here }; I am interested in making the export of MyModule universal, so that users can easily import it using any of the top 3 popular methods in the Browser: Using ES6 I ...