I have a question on rails validates. I need to validate that one field is filled out if another field has a certain value. The code is below, but if license has extended selected then the extended license type needs to be filled in. I have this but it isn''t working, when I fill the form out and select extended and don''t check anything for extended_license_type, it validates fine and I get no error message. validates :license, presence: true validates :extended_license_type, presence: true, :if => :license_extended? private def license_extended? license == ''extended'' end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Mu-xCQRQ45QJ. For more options, visit https://groups.google.com/groups/opt_out.
Seems to look okay, but maybe try removing "private" from the license_extended? method? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/yO245qhn6SAJ. For more options, visit https://groups.google.com/groups/opt_out.
Meant to add: this could be the problem since the Validator object cannot access the method on your model instance, so it''s always returning "false". -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/DDGmUgrvwFQJ. For more options, visit https://groups.google.com/groups/opt_out.
I actually figured this out. Looged at the debug console on my app and noticed that with the extended_license_type it was passing a ''0'' back since none of the check boxes were checked. My validates only checks for the presence of that field and ''0'' is something so that is what it was. On Thursday, January 3, 2013 8:52:08 AM UTC-7, Curtis Ovard wrote:> > I have a question on rails validates. I need to validate that one field is > filled out if another field has a certain value. The code is below, but if > license has extended selected then the extended license type needs to be > filled in. I have this but it isn''t working, when I fill the form out and > select extended and don''t check anything for extended_license_type, it > validates fine and I get no error message. > > validates :license, presence: true > validates :extended_license_type, presence: true, :if => > :license_extended? > > private > def license_extended? > license == ''extended'' > end >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/HpAWKbAwl9QJ. For more options, visit https://groups.google.com/groups/opt_out.