Rails - How to validate a field only if a another field has a certain value? -
Rails - How to validate a field only if a another field has a certain value? -
i'm pretty new rails , came across problem wasn't able solve friend google :)
in form have select 3 values: apple, banana , cherry. if take apple select hide select- , text-field javascript, because when apple chosen, there no need fill in these other 2 fields anymore.
so have problem validating form when it's submitted. i've found similar problems illustration in case of "only validate field if blank."
this problem solved this:
validates_presence_of :mobile_number, :unless => :home_phone?
so i've tried first thing popped mind:
validates_presence_of :state, :granted_at, :if => :type != 1
but when run it, error:
undefined method `validate' true:trueclass
so didn't find out how can access values object created... give thanks in advance help , hope question isn't obvious sounds :-)
because executable code need wrap in lambda or proc object so:
validates_presence_of :state, :granted_at, :if => lambda { |o| o.type != 1 } # alternatively: # :if => proc.new { |o| o.type != 1 }
ruby-on-rails ruby-on-rails-3 validation models
Comments
Post a Comment