Forum: Ruby on Rails conditional validation

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Paolo N. (Guest)
on 2006-06-12 13:36
(Received via mailing list)
Hi how can I validate a field only if another field is set to a specific

I tried to use

validates_length_of :fieldname, :maximum => 100, :if =>
:otherfieldname == 'myvalue'

But doesn't work


Daniel -. (Guest)
on 2006-06-12 14:11
(Received via mailing list)
You can use blocks in your validations.


validates_length_of :fieldname, :maximum => 100, :if =>{
mymodel.otherfieldname == 'myvaule' }
Paolo N. (Guest)
on 2006-06-12 15:15
(Received via mailing list)
Thanks Daniel

Of course this way it works.


2006/6/12, Daniel N <removed_email_address@domain.invalid>:
This topic is locked and can not be replied to.