简体   繁体   中英

How can I write a conditional validation in Rails?

Let's say I have a model, Post.rb:

validates :title => true
validates :post => true, :length => { :maximum => 220 } 
validate :custom_validation

def custom_validation 
   ...
end

I want to run the validtions for title, post, and the custom_validation ONLY if another attribute, draft, is false. (ie ignore when draft is true).

How can I do this?

validates :title => true, :unless => :draft

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM