From 343fbdb023e19661864d815592cdaddedfbf9664 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Mon, 9 Apr 2018 17:18:11 +0200
Subject: [PATCH] Clear error messages on form fields on change

The class "is-invalid" is removed on all type=text input
on keyup event.

JIRA INFRA-285
---
 app/static/js/csentry.js | 12 ++++++++++++
 app/static/js/hosts.js   |  4 ++++
 app/templates/base.html  |  1 +
 3 files changed, 17 insertions(+)
 create mode 100644 app/static/js/csentry.js

diff --git a/app/static/js/csentry.js b/app/static/js/csentry.js
new file mode 100644
index 0000000..9bf4c77
--- /dev/null
+++ b/app/static/js/csentry.js
@@ -0,0 +1,12 @@
+$(document).ready(function() {
+
+  // When an invalid input was submitted, the server
+  // adds the "is-invalid" class to form fields to display
+  // them in red with an error message
+  // When starting to type again, we want to remove the error
+  // message to not confuse the user
+  $('input[type="text"]').keyup(function(event) {
+    $(this).removeClass("is-invalid");
+  });
+
+});
diff --git a/app/static/js/hosts.js b/app/static/js/hosts.js
index c45f80d..1a38304 100644
--- a/app/static/js/hosts.js
+++ b/app/static/js/hosts.js
@@ -71,6 +71,10 @@ $(document).ready(function() {
   $("#name").keyup(function(event) {
     var hostname = $(this).val();
     $("#interface_name").val(hostname);
+    // If an invalid hostname was submitted, the interface name
+    // will be invalid as well
+    // Remove the invalid messages when starting typing a new name
+    $("#interface_name").removeClass("is-invalid");
   });
 
   // Fill MAC address on page load
diff --git a/app/templates/base.html b/app/templates/base.html
index d660cd4..0477e63 100644
--- a/app/templates/base.html
+++ b/app/templates/base.html
@@ -65,5 +65,6 @@
   <script type=text/javascript>
     $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
   </script>
+  <script src="{{ url_for('static', filename='js/csentry.js') }}"></script>
   {% block csentry_scripts %}{% endblock %}
 {% endblock %}
-- 
GitLab