Conditional validation

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

You can use blocks in your validations.

Try

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

Thanks Daniel

Of course this way it works.

Paolo

2006/6/12, Daniel N [email protected]: