From 9f1692e31f996bf75407354a5ac150284203571c Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Tue, 29 Dec 2020 21:06:31 +0100 Subject: [PATCH] Improved shopping list item form validation (closes #1226) --- localization/strings.pot | 3 +++ public/viewjs/shoppinglistitemform.js | 15 +++++++++++++++ views/components/productpicker.blade.php | 3 ++- views/shoppinglistitemform.blade.php | 7 +++++-- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/localization/strings.pot b/localization/strings.pot index e2f7d8c4..55139c2d 100644 --- a/localization/strings.pot +++ b/localization/strings.pot @@ -2074,3 +2074,6 @@ msgstr "" msgid "Print options" msgstr "" + +msgid "A product or a note is required" +msgstr "" diff --git a/public/viewjs/shoppinglistitemform.js b/public/viewjs/shoppinglistitemform.js index e6a6ea39..61ddce82 100644 --- a/public/viewjs/shoppinglistitemform.js +++ b/public/viewjs/shoppinglistitemform.js @@ -167,6 +167,9 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e) } ); } + + $("#note").trigger("input"); + $("#product_id").trigger("input"); }); Grocy.FrontendHelpers.ValidateForm('shoppinglist-form'); @@ -235,4 +238,16 @@ if (GetUriParam("embedded") !== undefined) } } +var eitherRequiredFields = $("#product_id,#product_id_text_input,#note"); +eitherRequiredFields.on("input", function() +{ + eitherRequiredFields.attr("required", ""); + if (!$(this).val().isEmpty()) + { + eitherRequiredFields.not(this).removeAttr("required"); + } + + Grocy.FrontendHelpers.ValidateForm('shoppinglist-form'); +}); + Grocy.Components.UserfieldsForm.Load(); diff --git a/views/components/productpicker.blade.php b/views/components/productpicker.blade.php index d9b37b04..59d33df2 100644 --- a/views/components/productpicker.blade.php +++ b/views/components/productpicker.blade.php @@ -11,6 +11,7 @@ @php if(!isset($disabled)) { $disabled = false; } @endphp @php if(empty($hint)) { $hint = ''; } @endphp @php if(empty($nextInputSelector)) { $nextInputSelector = ''; } @endphp +@php if(empty($validationMessage)) { $validationMessage = 'You have to select a product'; } @endphp
{{ $product->name }} @endforeach -
{{ $__t('You have to select a product') }}
+
{{ $__t($validationMessage) }}
$products, 'nextInputSelector' => '#amount', - 'isRequired' => false, - 'prefillById' => $productId + 'isRequired' => true, + 'prefillById' => $productId, + 'validationMessage' => 'A product or a note is required' ))
@@ -79,9 +80,11 @@
+
{{ $__t('A product or a note is required') }}
@include('components.userfieldsform', array(