Conditional validation


#1

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


#2

You can use blocks in your validations.

Try

validates_length_of :fieldname, :maximum => 100, :if => Proc.new{
|mymodel|
mymodel.otherfieldname == ‘myvaule’ }


#3

Thanks Daniel

Of course this way it works.

Paolo

2006/6/12, Daniel N removed_email_address@domain.invalid: