From 257a4008f8db9bbb260726a78acdeec081800f28 Mon Sep 17 00:00:00 2001 From: Aleksandar Petrushev Date: Wed, 22 Dec 2021 16:53:41 +0100 Subject: [PATCH] Use pattern to match numbers in product price fields --- app/helpers/spree/admin/base_helper.rb | 4 ++++ app/views/spree/admin/prices/_variant_prices.html.erb | 4 ++-- app/views/spree/admin/products/_form.html.erb | 6 +++--- app/views/spree/admin/products/new.html.erb | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/helpers/spree/admin/base_helper.rb b/app/helpers/spree/admin/base_helper.rb index 36d8d3057d..52284c1f17 100644 --- a/app/helpers/spree/admin/base_helper.rb +++ b/app/helpers/spree/admin/base_helper.rb @@ -303,6 +303,10 @@ def product_wysiwyg_editor_enabled? def taxon_wysiwyg_editor_enabled? Spree::Backend::Config[:taxon_wysiwyg_editor_enabled] end + + def number_validation_pattern + '^[0-9]*([.,][0-9]*)*$' + end end end end diff --git a/app/views/spree/admin/prices/_variant_prices.html.erb b/app/views/spree/admin/prices/_variant_prices.html.erb index 6d05a07d8b..bfa9ed2376 100644 --- a/app/views/spree/admin/prices/_variant_prices.html.erb +++ b/app/views/spree/admin/prices/_variant_prices.html.erb @@ -22,10 +22,10 @@ <%= variant.sku %> <%= label_tag "vp[#{variant.id}][#{code}]", code %> - <%= number_field_tag "vp[#{variant.id}][#{code}][price]", (price && price.price ? price.display_amount.money : ''), step: 0.01, min: 0, class: 'form-control' %> + <%= text_field_tag "vp[#{variant.id}][#{code}][price]", (price && price.price ? price.display_amount.money : ''), class: 'form-control', pattern: number_validation_pattern %> - <%= number_field_tag "vp[#{variant.id}][#{code}][compare_at_price]", (price && price.compare_at_price ? price.display_compare_at_amount.money : ''), step: 0.01, min: 0, class: 'form-control' %> + <%= text_field_tag "vp[#{variant.id}][#{code}][compare_at_price]", (price && price.compare_at_price ? price.display_compare_at_amount.money : ''), class: 'form-control', pattern: number_validation_pattern %> <% end %> diff --git a/app/views/spree/admin/products/_form.html.erb b/app/views/spree/admin/products/_form.html.erb index 6fc62ac68c..c3464f33a1 100644 --- a/app/views/spree/admin/products/_form.html.erb +++ b/app/views/spree/admin/products/_form.html.erb @@ -35,7 +35,7 @@
<%= currency_symbol(current_currency) %>
- <%= f.number_field :price, value: number_to_currency(@product.amount_in(current_currency), delimiter: '', unit: ''), step: 0.01, min: 0, class: 'form-control', required: :required, disabled: (cannot? :update, @product.master.default_price) %> + <%= f.text_field :price, value: number_to_currency(@product.amount_in(current_currency), unit: ''), class: 'form-control', disabled: (cannot? :update, @product.master.default_price), pattern: number_validation_pattern %> <%= f.error_message_on :price %> <% end %> @@ -48,7 +48,7 @@
<%= currency_symbol(current_currency) %>
- <%= f.number_field :compare_at_price, value: number_to_currency(@product.compare_at_amount_in(current_currency), delimiter: '', unit: ''), step: 0.01, min: 0, class: 'form-control' %> + <%= f.text_field :compare_at_price, value: number_to_currency(@product.compare_at_amount_in(current_currency), unit: ''), class: 'form-control', pattern: number_validation_pattern %> <%= f.error_message_on :compare_at_price %> <% end %> @@ -57,7 +57,7 @@
<%= f.field_container :cost_price do %> <%= f.label :cost_price, Spree.t(:cost_price) %> - <%= f.number_field :cost_price, value: number_to_currency(@product.cost_price, delimiter: '', unit: ''), step: 0.01, min: 0, class: 'form-control' %> + <%= f.text_field :cost_price, value: number_to_currency(@product.cost_price, unit: ''), class: 'form-control', pattern: number_validation_pattern %> <%= f.error_message_on :cost_price %> <% end %>
diff --git a/app/views/spree/admin/products/new.html.erb b/app/views/spree/admin/products/new.html.erb index 50f81170d2..655124cfa0 100644 --- a/app/views/spree/admin/products/new.html.erb +++ b/app/views/spree/admin/products/new.html.erb @@ -38,7 +38,7 @@
<%= currency_symbol(current_currency) %>
- <%= f.number_field :price, value: number_to_currency(@product.price, delimiter: '', unit: ''), step: 0.01, min: 0, class: 'form-control', required: :required %> + <%= f.text_field :price, value: number_to_currency(@product.price, unit: ''), class: 'form-control', required: :required, pattern: number_validation_pattern %> <%= f.error_message_on :price %> <% end %>