From fb50acddab61e558b677f8f284cc764bbce7471c Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Thu, 20 Dec 2018 18:57:23 -0700 Subject: [PATCH] Improve client-side validation for payment add/edit form (#20) --- pages.php | 5 ++++- pages/editpayment.php | 27 ++++++++++++++++++++------- static/js/editfamily.js | 19 ------------------- static/js/editpayment.js | 16 ++++++++++++++++ 4 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 static/js/editpayment.js diff --git a/pages.php b/pages.php index 3ab5200..c8ef1e1 100644 --- a/pages.php +++ b/pages.php @@ -50,7 +50,10 @@ define("PAGES", [ ] ], "editpayment" => [ - "title" => "Edit Payment" + "title" => "Edit Payment", + "scripts" => [ + "static/js/editpayment.js" + ] ], "events" => [ "title" => "Events", diff --git a/pages/editpayment.php b/pages/editpayment.php index 62eaf1f..21f6b38 100644 --- a/pages/editpayment.php +++ b/pages/editpayment.php @@ -31,7 +31,7 @@ if (!empty($_GET['id']) && $database->has('payments', ['paymentid' => $_GET['id' } ?> -
+
@@ -68,7 +68,8 @@ if (!empty($_GET['id']) && $database->has('payments', ['paymentid' => $_GET['id' "name" => "familyid", "type" => "select", "value" => $data["family"], - "options" => $familylist + "options" => $familylist, + "error" => "Choose a family." ], [ "label" => "Amount", @@ -77,16 +78,17 @@ if (!empty($_GET['id']) && $database->has('payments', ['paymentid' => $_GET['id' "type" => "number", "maxlength" => 5, "value" => $data["amount"], - "width" => 2 + "width" => 2, + "error" => "Enter a dollar amount." ], [ "label" => "Date", "icon" => "fas fa-calendar", "name" => "date", "type" => "date", - "maxlength" => 20, "value" => $data["date"], - "width" => 3 + "width" => 3, + "error" => "Choose a date for the payment." ], [ "label" => "Type", @@ -102,7 +104,8 @@ if (!empty($_GET['id']) && $database->has('payments', ['paymentid' => $_GET['id' "Free" => "Free", "Other" => "Other" ], - "width" => 3 + "width" => 3, + "error" => "Select a payment type." ] ]; @@ -124,6 +127,13 @@ if (!empty($_GET['id']) && $database->has('payments', ['paymentid' => $_GET['id' aria-label="" maxlength="" + pattern="" + + value="" @@ -148,6 +158,9 @@ if (!empty($_GET['id']) && $database->has('payments', ['paymentid' => $_GET['id' +
+ +
@@ -194,7 +207,7 @@ if (!empty($_GET['id']) && $database->has('payments', ['paymentid' => $_GET['id' ?> diff --git a/static/js/editfamily.js b/static/js/editfamily.js index ed21c5b..f175a39 100644 --- a/static/js/editfamily.js +++ b/static/js/editfamily.js @@ -14,25 +14,6 @@ $("#add_child_row").click(function () { }); }); -$("#editform").on("submit", function () { - // Do some client-side validation that isn't handled by the browser - var phone = $("input[name=phone]").val(); - var zip = $("input[name=zip]").val(); - var ok = true; - - - if (/^[0-9]{10}$/.test(phone) == false) { - ok = false; - $("input[name=phone]").addClass() - } - - if (/^[0-9]{5}(-?[0-9]{4})?$/.test(zip) == false) { - ok = false; - } - - return ok; -}); - $("#savebutton").click(function (event) { var form = $("#editform"); diff --git a/static/js/editpayment.js b/static/js/editpayment.js new file mode 100644 index 0000000..abfa584 --- /dev/null +++ b/static/js/editpayment.js @@ -0,0 +1,16 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + + +$("#savebutton").click(function (event) { + var form = $("#editform"); + + if (form[0].checkValidity() === false) { + event.preventDefault() + event.stopPropagation() + } + form.addClass('was-validated'); +}); \ No newline at end of file