EAN to string + integer validation

This commit is contained in:
Barnabé 2021-09-17 16:16:24 +02:00
parent 7dbed2374c
commit 3720120124
3 changed files with 9 additions and 3 deletions

View File

@ -18,7 +18,7 @@ class PProductRef < ApplicationRecord
accepts_nested_attributes_for :p_product_ref_specs, allow_destroy: true accepts_nested_attributes_for :p_product_ref_specs, allow_destroy: true
#validates :ct_price_ht, :presence => true #validates :ct_price_ht, :presence => true
validate :ean, :is_integer
has_many :p_customer_product_prices, :dependent => :destroy has_many :p_customer_product_prices, :dependent => :destroy
accepts_nested_attributes_for :p_customer_product_prices, allow_destroy: true accepts_nested_attributes_for :p_customer_product_prices, allow_destroy: true
@ -167,4 +167,10 @@ class PProductRef < ApplicationRecord
end end
end end
def is_integer
if ean.present? && ean.match(/\A[+-]?\d+\z/).nil?
errors.add(:ean, "ne doit être composé que de chiffres")
end
end
end end

View File

@ -1,5 +1,5 @@
class AddEanToPProductRefs < ActiveRecord::Migration[6.0] class AddEanToPProductRefs < ActiveRecord::Migration[6.0]
def change def change
add_column :p_product_refs, :ean, :integer add_column :p_product_refs, :ean, :string
end end
end end

View File

@ -1799,7 +1799,7 @@ ActiveRecord::Schema.define(version: 2021_09_17_124338) do
t.decimal "ca_dee", precision: 10 t.decimal "ca_dee", precision: 10
t.decimal "cc_dee", precision: 10 t.decimal "cc_dee", precision: 10
t.decimal "ct_dee", precision: 10 t.decimal "ct_dee", precision: 10
t.integer "ean" t.string "ean"
t.string "sorecop_comment" t.string "sorecop_comment"
end end