Skip to content

Commit

Permalink
Use pattern to match numbers in product price fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandar Petrushev committed Dec 23, 2021
1 parent 3999dd4 commit 257a400
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/helpers/spree/admin/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/views/spree/admin/prices/_variant_prices.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
<td class="panel-title"><%= variant.sku %></td>
<td><%= label_tag "vp[#{variant.id}][#{code}]", code %></td>
<td>
<%= 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 %>
</td>
<td>
<%= 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 %>
</td>
</tr>
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/spree/admin/products/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<div class="input-group-prepend">
<span class="input-group-text"><%= currency_symbol(current_currency) %></span>
</div>
<%= 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 %>
</div>
<%= f.error_message_on :price %>
<% end %>
Expand All @@ -48,7 +48,7 @@
<div class="input-group-prepend">
<span class="input-group-text"><%= currency_symbol(current_currency) %></span>
</div>
<%= 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 %>
</div>
<%= f.error_message_on :compare_at_price %>
<% end %>
Expand All @@ -57,7 +57,7 @@
<div data-hook="admin_product_form_cost_price" class="alpha two columns">
<%= 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 %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/admin/products/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<div class="input-group-prepend">
<span class="input-group-text"><%= currency_symbol(current_currency) %></span>
</div>
<%= 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 %>
</div>
<%= f.error_message_on :price %>
<% end %>
Expand Down

0 comments on commit 257a400

Please sign in to comment.