Don't validate numericality if blank

This avoids unnecessary second message when left blank:
> can't be blank
> is not a number

Ok this is a little confusing. Why is there a  separate presence check above, and why is it only for measurable units, when we still require a number for _all_ units? Because, for 'items', we allow a blank value then auto-set it to 1.
I don't know if it's really necessary, but that's how it currently works...
This commit is contained in:
David Cook
2024-02-15 11:26:04 +11:00
parent 915a5ce66b
commit 7164cbd0bd
2 changed files with 3 additions and 3 deletions

View File

@@ -69,7 +69,7 @@ module Spree
%w(weight volume).include?(variant.product&.variant_unit)
}
validates :unit_value, numericality: { greater_than: 0 }
validates :unit_value, numericality: { greater_than: 0 }, allow_blank: true
validates :price, numericality: { greater_than_or_equal_to: 0 }
validates :unit_description, presence: true, if: ->(variant) {

View File

@@ -307,7 +307,7 @@ describe '
click_button 'Save Changes', match: :first
expect(page.find("#status-message"))
.to have_content "Variant unit value can't be blank Variant unit value is not a number"
.to have_content "Variant unit value can't be blank"
end
it "creating a variant with unit value is: '120g' and 'on_demand' checked" do
@@ -323,7 +323,7 @@ describe '
click_button 'Save Changes', match: :first
expect(page.find("#status-message"))
.to have_content "Variant unit value can't be blank Variant unit value is not a number"
.to have_content "Variant unit value can't be blank"
end
end
end