Hairi
Hairi

Reputation: 3715

Override validation in Rails model

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?

Upvotes: 0

Views: 832

Answers (1)

Joserra Díaz
Joserra Díaz

Reputation: 21

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

Upvotes: 0

Related Questions