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'' }
On 6/12/06, Paolo Negri <hungrylist@gmail.com>
wrote:>
> 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
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060612/0802125d/attachment-0001.html
Thanks Daniel Of course this way it works. Paolo 2006/6/12, Daniel N <has.sox@gmail.com>:> You can use blocks in your validations. > > Try > > validates_length_of :fieldname, :maximum => 100, :if => Proc.new{ |mymodel| > mymodel.otherfieldname == ''myvaule'' } > > > > > On 6/12/06, Paolo Negri <hungrylist@gmail.com> wrote: > > > 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