From 712c204bcafbc68a66da894e48ae9fbc2bbf9b46 Mon Sep 17 00:00:00 2001 From: Meron Ogbai Date: Sun, 5 Sep 2021 01:09:03 +0300 Subject: [PATCH 1/2] Add error message in en.yml --- config/locales/en.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/locales/en.yml b/config/locales/en.yml index 98f1ded47f..96eda40057 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2308,6 +2308,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using calculator_values: "Calculator values" calculator_settings_warning: "If you are changing the calculator type, you must save first before you can edit the calculator settings" calculator_preferred_unit_error: "must be kg or lb" + calculator_preferred_amount_error: "Invalid input. Please use only numbers. For example: 10, 5.5, -20" flat_percent_per_item: "Flat Percent (per item)" flat_rate_per_item: "Flat Rate (per item)" flat_rate_per_order: "Flat Rate (per order)" From 5ca552d380af21dad6fb234eaaebbcc3856883e7 Mon Sep 17 00:00:00 2001 From: Meron Ogbai Date: Sun, 5 Sep 2021 01:11:12 +0300 Subject: [PATCH 2/2] Show flash error if preferred amount isn't valid --- .../spree/admin/payment_methods_controller.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/controllers/spree/admin/payment_methods_controller.rb b/app/controllers/spree/admin/payment_methods_controller.rb index 72ac7a7a35..e4d1d8f3ed 100644 --- a/app/controllers/spree/admin/payment_methods_controller.rb +++ b/app/controllers/spree/admin/payment_methods_controller.rb @@ -7,6 +7,7 @@ module Spree before_action :load_data before_action :validate_payment_method_provider, only: [:create] before_action :load_hubs, only: [:new, :edit, :update] + before_action :validate_calculator_preferred_amount, only: [:update] respond_to :html @@ -173,6 +174,16 @@ module Spree params_for_update end end + + def validate_calculator_preferred_amount + preferred_amount = params.dig(:payment_method_check, :calculator_attributes, + :preferred_amount) + return if preferred_amount.nil? || Float(preferred_amount, + exception: false) + + flash[:error] = I18n.t(:calculator_preferred_amount_error) + redirect_to spree.edit_admin_payment_method_path(@payment_method) + end end end end