mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-17 00:07:24 +00:00
Add errors for invalid input with number localization enabled
This commit is contained in:
@@ -44,7 +44,7 @@ module Spree
|
||||
|
||||
def self.valid_localizable_number?(number)
|
||||
return true unless number.is_a?(String) || number.respond_to?(:to_d)
|
||||
return false if number.to_s =~ /[.,]\d{2}[.,]/
|
||||
return false if number.to_s =~ /[.,]\d{2}[.,]/ || number.to_s =~ /[^0-9,.]+/
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
@@ -59,5 +59,11 @@ describe Spree::LocalizedNumber do
|
||||
expect(described_class.valid_localizable_number?(1599.99)).to eql true
|
||||
end
|
||||
end
|
||||
|
||||
context "with letters" do
|
||||
it "returns false" do
|
||||
expect(described_class.valid_localizable_number?('invalid')).to eql false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -45,4 +45,21 @@ shared_examples "a parent model that has a Calculator" do |parent_name|
|
||||
expect(error_messages).not_to include(/^Calculator preferred/)
|
||||
end
|
||||
end
|
||||
|
||||
context "when number localization is enabled and the associated Calculator is invalid" do
|
||||
let(:localized_parent) do
|
||||
build(parent_name, calculator: Calculator::FlatRate.new(preferred_amount: "invalid"))
|
||||
end
|
||||
|
||||
before do
|
||||
allow(Spree::Config).to receive(:enable_localized_number?).and_return true
|
||||
localized_parent.valid?
|
||||
end
|
||||
|
||||
it "adds custom error messages to base" do
|
||||
expect(localized_parent.errors[:base]).to include(
|
||||
/#{I18n.t('spree.localized_number.invalid_format')}/
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user