Hi, I am having problems getting the allow_nil to allow nil. I have a password field with the following validation: validates_length_of :password, :minimum => 6, :allow_nil => true If I submit a form without entering the password, it comes in as (printed via debug): password: '''' The validation kicks in for the length and says it is too short. Any ideas are appreciated. Thanks, Tom
On Tue, Sep 13, 2005, Tom Davies wrote:> Hi, > > I am having problems getting the allow_nil to allow nil. > > I have a password field with the following validation: > > validates_length_of :password, :minimum => 6, :allow_nil => true > > If I submit a form without entering the password, it comes in as > (printed via debug): > > password: '''' > > The validation kicks in for the length and says it is too short.'''' is an empty string, and if I''m not mistaken, allow_nil only allows the nil value. You may need to munge your empty string to nil before validation. I could be wrong though :) Ben
I have a feeling you are right.... I guess the alternative is that it might overwrite the password to nothing... but part of me wishes the empty string is treated equivalent to nil. Oh well... I will just grab it and nillify it before validation. Thanks, Tom On 9/13/05, Ben Bleything <ben-TGHtUsa5cOzMFIMGWPqnnw@public.gmane.org> wrote:> On Tue, Sep 13, 2005, Tom Davies wrote: > > Hi, > > > > I am having problems getting the allow_nil to allow nil. > > > > I have a password field with the following validation: > > > > validates_length_of :password, :minimum => 6, :allow_nil => true > > > > If I submit a form without entering the password, it comes in as > > (printed via debug): > > > > password: '''' > > > > The validation kicks in for the length and says it is too short. > > '''' is an empty string, and if I''m not mistaken, allow_nil only allows > the nil value. You may need to munge your empty string to nil before > validation. > > I could be wrong though :) > > Ben >
Tom Davies wrote:> Oh well... I will just grab it and nillify it before validation.Wouldn''t it be nice if validates_length_of validated the length of the attached object if it was present and, if not, did nothing else?
I agree that it would be more intuitive to work that way. However, I doubt it will change since it would break backwards compatibility if all of a sudden the empty string started getting through. This might also incorrectly set some properties to the empty string too. Perhaps their should just be a nice method to convert the params to nil like: for controllers parameters: before_filter :empty_to_nil for models attributes before_validation :empty_to_nil We could also add :only, and :except tags. Not sure how useful this sort of thing is for others though. Tom On 9/13/05, Nicholas Seckar <nseckar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Tom Davies wrote: > > > Oh well... I will just grab it and nillify it before validation. > > Wouldn''t it be nice if validates_length_of validated the length of the > attached object if it was present and, if not, did nothing else? >
>> but part of me wishes the empty string is treated equivalent to nil.Perhaps its better to wish for :allow_empty => true ? On 9/13/05, Ben Bleything <ben-TGHtUsa5cOzMFIMGWPqnnw@public.gmane.org> wrote:> On Tue, Sep 13, 2005, Tom Davies wrote: > > Hi, > > > > I am having problems getting the allow_nil to allow nil. > > > > I have a password field with the following validation: > > > > validates_length_of :password, :minimum => 6, :allow_nil => true > > > > If I submit a form without entering the password, it comes in as > > (printed via debug): > > > > password: '''' > > > > The validation kicks in for the length and says it is too short. > > '''' is an empty string, and if I''m not mistaken, allow_nil only allows > the nil value. You may need to munge your empty string to nil before > validation. > > I could be wrong though :) > > Ben >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Maybe http://wiki.rubyonrails.com/rails/show/HowToStripWhitespaceFromModelFields would help you? Dan
Thanks Dan. That is just what I was looking for. On 9/14/05, Dan Sketcher <dansketcher-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Maybe http://wiki.rubyonrails.com/rails/show/HowToStripWhitespaceFromModelFields > would help you? > > Dan > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >