Forum: Ruby on Rails conditional validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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
value?

I tried to use

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

But doesn't work

Thanks

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

Try

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

Of course this way it works.

Paolo

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