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
on 2013-01-03 18:20
on 2013-01-03 19:05
Seems to look okay, but maybe try removing "private" from the license_extended? method?
on 2013-01-03 19:09
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".
on 2013-01-04 17:01
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.