I''d like to validate that the length of text is within a specified range, but only if the text is not nil. To do this, I''d like to setup a custom validation: def validate_on_update validate_within_range_if_not_nil end Where validate_within_range_if_not_nil would first check if the text is nil, and if not, then use the validation helper #validates_length_of. However, I don''t know how to call this method from within a custom validation method. I tried: ActiveRecord::Validations::ClassMethods::validates_length_of But received a NoMethodError. Any ideas on implementing this type of validation? -- Derek Haynes HighGroove Studios - http://www.highgroove.com Keeping it Simple. 404.593.4879
Hi Rails can handle that validation straight out of the box, and in one line too: validates_length_of :variable, :within => 3..40, :allow_nil => true, :on => :update Regards Elliot On 09/09/05, Derek Haynes <derek.haynes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''d like to validate that the length of text is within a specified > range, but only if the text is not nil. > > To do this, I''d like to setup a custom validation: > > def validate_on_update > validate_within_range_if_not_nil > end > > Where validate_within_range_if_not_nil would first check if the text > is nil, and if not, then use the validation helper > #validates_length_of. > > However, I don''t know how to call this method from within a custom > validation method. I tried: > > ActiveRecord::Validations::ClassMethods::validates_length_of > > But received a NoMethodError. > > Any ideas on implementing this type of validation? > -- > Derek Haynes > HighGroove Studios - http://www.highgroove.com > Keeping it Simple. > 404.593.4879 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Elliot, Perfect! But lets say it couldn''t - is there a way to access the helper method? - Derek On 9/9/05, Elliot Bowes <eblists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > Rails can handle that validation straight out of the box, and in one line too: > > validates_length_of :variable, :within => 3..40, :allow_nil => true, > :on => :update > > Regards > > Elliot > > On 09/09/05, Derek Haynes <derek.haynes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''d like to validate that the length of text is within a specified > > range, but only if the text is not nil. > > > > To do this, I''d like to setup a custom validation: > > > > def validate_on_update > > validate_within_range_if_not_nil > > end > > > > Where validate_within_range_if_not_nil would first check if the text > > is nil, and if not, then use the validation helper > > #validates_length_of. > > > > However, I don''t know how to call this method from within a custom > > validation method. I tried: > > > > ActiveRecord::Validations::ClassMethods::validates_length_of > > > > But received a NoMethodError. > > > > Any ideas on implementing this type of validation? > > -- > > Derek Haynes > > HighGroove Studios - http://www.highgroove.com > > Keeping it Simple. > > 404.593.4879 > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Derek Haynes HighGroove Studios - http://www.highgroove.com Keeping it Simple. 404.593.4879
Or something like what I am facing, I have three fields in a record where it is all or nothing for these fields. So I want something along the lines of: if var1 == nill || var2 == nil || var3 == nil # all three must be nil - Michael On 9/9/05, Derek Haynes <derek.haynes-cRsPOODq9VkXQ3Lr6voeyA@public.gmane.org> wrote:> Elliot, > > Perfect! But lets say it couldn''t - is there a way to access the helper method? > > - Derek > > On 9/9/05, Elliot Bowes <eblists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi > > > > Rails can handle that validation straight out of the box, and in one line too: > > > > validates_length_of :variable, :within => 3..40, :allow_nil => true, > > :on => :update > > > > Regards > > > > Elliot > > > > On 09/09/05, Derek Haynes <derek.haynes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''d like to validate that the length of text is within a specified > > > range, but only if the text is not nil. > > > > > > To do this, I''d like to setup a custom validation: > > > > > > def validate_on_update > > > validate_within_range_if_not_nil > > > end > > > > > > Where validate_within_range_if_not_nil would first check if the text > > > is nil, and if not, then use the validation helper > > > #validates_length_of. > > > > > > However, I don''t know how to call this method from within a custom > > > validation method. I tried: > > > > > > ActiveRecord::Validations::ClassMethods::validates_length_of > > > > > > But received a NoMethodError. > > > > > > Any ideas on implementing this type of validation? > > > -- > > > Derek Haynes > > > HighGroove Studios - http://www.highgroove.com > > > Keeping it Simple. > > > 404.593.4879 > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > -- > Derek Haynes > HighGroove Studios - http://www.highgroove.com > Keeping it Simple. > 404.593.4879 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >