0

I have a spree model that has the following validations:

with_options presence: true do
  validates :firstname, :lastname, :address1, :city, :country
  validates :zipcode, if: :require_zipcode?
  validates :phone, if: :require_phone?
end

I would like to remove the city and/or country from presence validation. In my address_decorator I wrote this:

Spree::Address.class_eval do

  with_options presence: true do
    validates :firstname, :lastname, :address1
    validates :zipcode, if: :require_zipcode?
    validates :phone, if: :require_phone?
  end
......

But this didn't remove city or country. Both are still demanded in order to create new record.

What am I missing?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hairi
  • 3,318
  • 2
  • 29
  • 68

1 Answers1

0

When you add "with_options" you are not removing the previous validations, you are just adding more.

So, there is to remove the validations for :city and :country

Not checked myself, but I´ve seen something similar to:

.class_eval do
  _validators.reject{ |key, _| key == :field }

  _validate_callbacks.reject do |callback|
    callback.raw_filter.attributes == [:field]
  end

where :field is :city and :country

  • Thanks but I get error: undefined method attributes. On the other hand this approach helped: https://stackoverflow.com/a/24793039/5598574 – Hairi May 12 '20 at 07:46