2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spreeでモデルの特定カラムのvalidatesを外す

Last updated at Posted at 2016-12-21

ちゃお。田中です。SpreeのAddressモデルの phone を必須入力させないように四苦八苦してたので共有します。
モデルのカスタマイズについては公式のサイトにあるので割合します。:santa:

app/models/spree/address_decorator.rb
module Spree
  Address.class_eval do

      #validates :phone, presence: true, if: :require_phone?

  end
end

こんな感じでコメントアウトでいけるだろっー!って思っていたけど無理でした(笑

やり方は以下のような感じです。

app/models/spree/address_decorator.rb
module Spree
  Address.class_eval do

      _validators.reject!{ |key, value| key == :phone }
      _validate_callbacks.each do |callback|
        callback.raw_filter.attributes.reject! { |key| key == :phone } if callback.raw_filter.respond_to?(:attributes)
      end

  end
end
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?