Doing a rails custom validator with-out using validates_each

Posted by Tim Connor Tue, 20 Nov 2007 03:01:00 GMT

I wanted to do a custom validator (forthcoming post) that validates that at least one out of a list of attributes is present (validates_presence_of_any). Unfortunately, all the examples I could find use validates_each (see here for a decent example on the topic in general), and since mine depends on the list as a whole, that doesn’t quite fit. So I just stole some of the guts of validates each, to get the magic reference to the record object. As a bonus, then my validation conforms to other validation conventions, like accepting :if => in an options hash.
  def self.validates_your_funky_non_validates_each_using_way(*attrs)
    options = attrs.last.is_a?(Hash) ? attrs.pop.symbolize_keys : {}
    attrs   = attrs.flatten
    # Declare the validation.
    send(validation_method(options[:on] || :save)) do |record|
      return if options[:if] && !evaluate_condition(options[:if], record)
       #put real code using attrs and record here
      record.errors.add :base, "monkey", if record.primate?
    end
  end
Comment form

(leave url/email »)

Help with Textile (code)