This project is archived and is in readonly mode.

#1512 ✓wontfix
Gerald Boersma

Enhance :if argument for ActiveRecord validation methods

Reported by Gerald Boersma | December 3rd, 2008 @ 09:07 AM | in 2.x

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)]

Comments and changes to this ticket

  • josh

    josh December 3rd, 2008 @ 03:39 PM

    • 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
    
  • Gerald Boersma

    Gerald Boersma December 3rd, 2008 @ 05:34 PM

    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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages