Validation with :if params

Hi all!
I’m trying to make a model validation that starts only if a given
attribute has a specified value, but it seems i’m doing something wrong
:slight_smile:

I have 3 values: respond_via, email, telephone_number. I want the latter
ones to validate only if respond_via has specific value.

So far i have:
validates_presence_of :email, :if => (:respond_via == “e-mail”)
validates_presence_of :telephone_number, :if => (:respond_via ==
“telephone”)

, but this version raises undefined method `respond_via’ exception.

I am sure it is defined, so it must be my syntax that is to blame. Any
suggestions?

On 10/23/07, Mind M. [email protected] wrote:

validates_presence_of :email, :if => (:respond_via == “e-mail”)
validates_presence_of :email :if => Proc.new {|model|
model.respond_via == “e_mail”}


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Rick Denatale wrote:

validates_presence_of :email :if => Proc.new {|model|
model.respond_via == “e_mail”}

Thanks a lot, this worked :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs