Hello everyone! I noticed that the validation shortcuts basically don''t trim the input that they validate, so validates_presence_of is kinda useless to me (the user can type in 5 spaces and bypass validation). This seems kinda strange to me, BTW, maybe there can be a symbol for that like validates_presence_of :name, :strip=>true Is it possible to somehow "hack" into AR''s validations to make them strip the input before validation and when saving the data? Without resorting to regexps etc. -- Julian "Julik" Tarkhanov
On 15-mei-2005, at 22:09, Robert Mannl wrote:> > >> validates_presence_of :name, :strip=>true >> > > Stripping should be default.+1 However, in the meantime I called The Dark Gods Of Overriding to the rescue and came up with something _very_very_ nasty, but it works. # The method "nil?" on Strings is used by ActiveRecord to check if a string has some content. We juice it up. class String def nil? self.strip.size === 0 end end God bless Ruby. Anyone has any ideas about a more elegant solution? I think this question deserves a ticket - stripping whitespace is just something too obvious to be missed by AR. -- Julian "Julik" Tarkhanov
On 15-mei-2005, at 15:16, Julian ''Julik'' Tarkhanov wrote:> > However, in the meantime I called The Dark Gods Of Overriding to > the rescue and came up with something _very_very_ nasty, but it works.Well, it was indeed "nasty" - with this sort of check you have to type return twice in IRB... I thought for a while how can I do it more elegantly and came up with this class WhitespaceKiller < ActiveRecord::Observer observe News, Work, Client, Type, Activity def before_validation(record) record.attributes.each do | attr, val| if !val.nil? && val.respond_to?(:strip) s = val.strip s.size == 0? record[attr] = nil : record[attr] = s end end end end I put it into lib/whitespace_killer.rb and then load it and instantiate it **manually** in my environment: require File.dirname(__FILE__) + ''/../lib/whitespace_killer'' wk = WhitespaceKiller.instance After that it effectively cleans every attribute that can be cleaned, and also get the validations triggered if a string has no characters and the validation is supposed to catch nil. -- Julian "Julik" Tarkhanov
> validates_presence_of :name, :strip=>trueStripping should be default. One should rather do> validates_presence_of :name, :strip=>falseif one doesn''t want any white-space stripping (but I''m not a RoR coder, just throwing in my opinion)
Hi! On Sun, 15 May 2005, Julian ''Julik'' Tarkhanov wrote the following:> I noticed that the validation shortcuts basically don''t trim the > input that they validate, so validates_presence_of is kinda useless > to me (the user can type in 5 spaces and bypass validation). This > seems kinda strange to me, BTW, maybe there can be a symbol for that > likePlease file a ticket here: http://dev.rubyonrails.com/ kind regards Wolfgang
Wolfgang Klinger <wolfgang@...> writes:> > > Hi! > > On Sun, 15 May 2005, Julian ''Julik'' Tarkhanov wrote the following: > > I noticed that the validation shortcuts basically don''t trim the > > input that they validate, so validates_presence_of is kinda useless > > to me (the user can type in 5 spaces and bypass validation). This > > seems kinda strange to me, BTW, maybe there can be a symbol for that > > like > > Please file a ticket here: > http://dev.rubyonrails.com/ > > kind regards > Wolfgang >I''m having a similar issue. I''ve got a model with validates_presence_of in it and the SQL schema has the field in question set to NOT NULL... however in both the new and edit views leaving the textbox empty results in the dreaded ''cannot render nil template''. I''ve got the validation rule set as well as the NOT NULL qualifier... what more do I need to ensure the field has data in it? Not worried about the space bug the original poster was talking about, want to get this worked out before proceeding. Thanks!
On 16-mei-2005, at 14:24, Wolfgang Klinger wrote:> > Please file a ticket here: > http://dev.rubyonrails.com/Presto. http://dev.rubyonrails.com/ticket/1309 I think any kind of feedback kan be beneficial because the solution to this depends of AR and AP ''ideology'' (where to handle what) and any kind of "upright" improvements will likely be rejected based on one or another religious aspect :-) -- Julian "Julik" Tarkhanov
see my response here: http://dev.rubyonrails.com/ticket/1309 _a> Presto. http://dev.rubyonrails.com/ticket/1309 > I think any kind of feedback kan be beneficial because the solution to > this depends of AR and AP ''ideology'' (where to handle what) and any > kind of "upright" improvements will likely be rejected based on one or > another religious aspect :-)-- alex black, founder the turing studio, inc. 510.666.0074 root-16h2cdTTKgpzNNFeSAH1EA@public.gmane.org http://www.turingstudio.com 2600 10th street, suite 635 berkeley, ca 94710