From a0c2facca4bb9bcc00c08d2cd2680f4d86591c65 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 27 Nov 2023 15:44:42 +0100 Subject: [PATCH] validate price numericality on product and variants --- app/models/spree/product.rb | 1 + app/models/spree/variant.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index a8cb206056..41b8c8f610 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -67,6 +67,7 @@ module Spree validates :variant_unit_name, presence: { if: ->(p) { p.variant_unit == 'items' } } validate :validate_image + validates :price, numericality: { greater_than_or_equal_to: 0, if: ->{ new_record? } } accepts_nested_attributes_for :variants, allow_destroy: true accepts_nested_attributes_for :image diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index ea05767a63..f470f4bb51 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -72,6 +72,7 @@ module Spree } validates :unit_value, numericality: { greater_than: 0 } + validates :price, numericality: { greater_than_or_equal_to: 0 } validates :unit_description, presence: true, if: ->(variant) { variant.product&.variant_unit.present? && variant.unit_value.nil?