Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Enhance :if argument for ActiveRecord validation methods

#1512

This is an enhancement request.

The provided ActiveRecord validation methods (e.g. validates_presence_of) accept :if as a parameter. However, it only accepts a single symbol (or a Proc or string).

I have a case where I want the validation rule to fire in one of a number of possible cases. Rather than having to define additional methods that combine the tests into one method call (in order to identify as single symbol), or a verbose and not clean-looking Proc method, I would like to pass an array of symbols. If any one of the parameters returns true, the validation fires.

e.g.

Only fire the validation rule for the first page of new form, second page of edit form, or if not in a form (i.e. all other cases).

validates_presence_of :name, :if => [:not_in_form?, :in_form?("new", 1), :in_form?("new", 2)]

Reported by Gerald Boersma · December 3rd, 2008 @ 09:07 AM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. josh
    josh
    • State changed from new to wontfix

    I don't think those symbols with arguments are valid ruby syntax.

    I would suggest just going the simple route and doing something like this,

    
    validates_presence_of :name, :if => :in_correct_form
    
    def in_correct_form
      ...
    end
    

    December 3rd, 2008 @ 03:39 PM

  2. Gerald Boersma
    Gerald Boersma

    Joshua:

    Of course. That won't work! I agree with your resolution for this one.

    Just getting to know my way around Ruby, so I missed that one. Thanks for your help.

    Cheers, Gerald

    December 3rd, 2008 @ 05:34 PM