From 68d04d418f688ac59bd1f0284186132a6cce24e6 Mon Sep 17 00:00:00 2001 From: gb0101010101 <7369439+gb0101010101@users.noreply.github.com> Date: Wed, 6 May 2020 19:58:40 -0700 Subject: [PATCH] gb0101010101-decimal-quick-fix: Enforce form validation on AJAX submits. Create jinja2 macro to generate decimal form inputs, add custom JS validation for input type 'number' that uses attribute 'pattern' which is not natively supported in HTML5. --- static/scripts/frontpage3d.js | 21 +++++++++++++ static/scripts/frontpage3dcontrols.js | 8 +++++ static/scripts/frontpageControls.js | 32 ++++++++++++++----- static/scripts/settings.js | 7 ++++- templates/editBoard.html | 12 +++---- templates/editBoard_mobile.html | 12 +++---- templates/frontpage3d.html | 6 ++-- templates/frontpage3d_mobile.html | 4 +-- templates/frontpage3d_mobilecontrols.html | 4 +-- templates/holeyCalibration.html | 26 ++++++++-------- templates/macros.html | 5 +++ templates/opticalCalibration.html | 38 ++++++++++++----------- templates/pidTuning.html | 32 +++++++++---------- templates/quickConfigure.html | 7 +++-- templates/settings.html | 2 +- templates/settings_mobile.html | 2 +- templates/triangularCalibration.html | 10 +++--- templates/trimBoard.html | 10 +++--- templates/zaxis.html | 4 +-- templates/zaxis_mobile.html | 4 +-- 20 files changed, 153 insertions(+), 93 deletions(-) create mode 100644 templates/macros.html diff --git a/static/scripts/frontpage3d.js b/static/scripts/frontpage3d.js index 7491e3e8..1a625682 100644 --- a/static/scripts/frontpage3d.js +++ b/static/scripts/frontpage3d.js @@ -807,3 +807,24 @@ function toggle3DPO(){ } } +function onFooterSubmit(){ + var form = $("#settingsForm") + // JQuery returns list of forms so must select first [0]. + isValid = form[0].checkValidity(); + if (isValid) { + var url = $("#pageID").val() + $.ajax({ + url : '/'+url, + type: "POST", + data: form.serialize(), + success: function (data) { + console.log("success"); + $('#contentModal').modal('toggle') + }, + error: function (jXHR, textStatus, errorThrown) { + alert(errorThrown); + } + }); + } +} + diff --git a/static/scripts/frontpage3dcontrols.js b/static/scripts/frontpage3dcontrols.js index d55cf71b..fb2560f8 100644 --- a/static/scripts/frontpage3dcontrols.js +++ b/static/scripts/frontpage3dcontrols.js @@ -152,3 +152,11 @@ function processStatusMessage(data){ else $("#currentPositioningMode").text("Incremental (G91)"); } + +function inputDecimalValidate() { + var re = new RegExp($(this).attr("pattern")); + valid = pattern.test($(this).val()); + if (!valid) { + $(this).setCustomValidity("Decimal input pattern mismatch"); + } +} diff --git a/static/scripts/frontpageControls.js b/static/scripts/frontpageControls.js index dc0bb784..42aeb37f 100644 --- a/static/scripts/frontpageControls.js +++ b/static/scripts/frontpageControls.js @@ -22,6 +22,25 @@ $(document).ready(function(){ var controllerMessage = document.getElementById('controllerMessage'); controllerMessage.scrollTop = controllerMessage.scrollHeight; $("#stickyButtons").css("top", $(".navbar").outerHeight()); + + // Set custom validation on all form inputs of type 'number' with attribute 'pattern'. + // Input type 'number' does not support 'pattern' attribute by default using HTML5. + // Input type 'number' always returns a value compatible with parseFloat using period as decimal separator. + // @todo: This event handler may need to be moved to other JS files for more/less global application. + $(':input[type="number"][pattern]').change(function () { + var re = new RegExp($(this).attr("pattern")); + var isValid = re.test($(this).val()); + // JQuery selector returns array of possible matches. + // Validity checks must be applied to first item [0]. + if (isValid) { + // Set empty value to clear error. + $(this)[0].setCustomValidity(""); + } + else { + // Set title as error message. + $(this)[0].setCustomValidity($(this).attr("title")); + } + }); }); function pauseRun(){ @@ -138,13 +157,12 @@ function boardDataUpdate(data){ } function moveAction(direction) { - distance = $("#distToMove").val(); - distanceValid = distance.search(/^[0-9]*(\.[0-9]{0,3})?$/); - if (distanceValid == 0) { - action('move', direction, distance); - } else { - $("#distToMove").focus(); - } + // JQuery returns list of matching forms so select first [0] for validitity checking. + var isValid = $("#distInput")[0].checkValidity(); + if (isValid) { + distance = $("#distToMove").val(); + action('move', direction, distance); + } } function processStatusMessage(data){ diff --git a/static/scripts/settings.js b/static/scripts/settings.js index ea7a5472..45271612 100644 --- a/static/scripts/settings.js +++ b/static/scripts/settings.js @@ -1,9 +1,11 @@ function onFooterSubmit(){ + var form = $("#settingsForm") + if (form[0].checkValidity()) { var url = $("#pageID").val() $.ajax({ url : '/'+url, type: "POST", - data: $("#settingsForm").serialize(), + data: form.serialize(), success: function (data) { console.log("success"); $('#contentModal').modal('toggle') @@ -12,8 +14,10 @@ function onFooterSubmit(){ alert(errorThrown); } }); + } } +/* $(document).ready(function () { $('#settingsForm').on('submit', function(e) { e.preventDefault(); @@ -38,6 +42,7 @@ $(document).ready(function () { }); }); +*/ function updatePorts(data){ var selectedPort = $("#comPorts").find(":selected").text(); diff --git a/templates/editBoard.html b/templates/editBoard.html index d0faf3c2..b0ab1c09 100644 --- a/templates/editBoard.html +++ b/templates/editBoard.html @@ -1,4 +1,4 @@ - +{% import 'macros.html' as macros %} {% block content %}