Hey guys, I''m using ''validates_format_of'', ''validates_length_of'', and ''validates_uniqueness_of'' on certain fields in my DB but these fields are not necessarily required. However, if you leave them blank the validators will kick in and block submission. Is there a way I can add exceptions to the built in validation helpers? Also, these fields are presently ''not null'' in the database. If I switch them to allow null will rails automatically allow for the fields to be blank? Sorry ORML not only adds a new sense of ease, but a new dimension of complexity for me at the same time :) - Jim
On 03/10/2005, at 11:35 AM, Jim Jeffers wrote:> I''m using ''validates_format_of'', ''validates_length_of'', and > ''validates_uniqueness_of'' on certain fields in my DB but these > fields are not necessarily required. However, if you leave them > blank the validators will kick in and block submission. Is there a > way I can add exceptions to the built in validation helpers?You''re thinking too much :) http://api.rubyonrails.com/classes/ActiveRecord/Validations/ ClassMethods.html#M000642 validates_length_of() (for example) has an :allow_nil option, which will skip validation if the attribute is nil. Straight from the examples in the docs: validates_length_of :fax, :in => 7..32, :allow_nil => true There''s also :if which can have a method which can be called to determine if validation should kick in or not. Regards, Justin French --- TextDrive.com Indent.com.au JustinFrench.com
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 2, 2005, at 6:35 PM, Jim Jeffers wrote:> I''m using ''validates_format_of'', ''validates_length_of'', and > ''validates_uniqueness_of'' on certain fields in my DB but these > fields are not necessarily required. However, if you leave them > blank the validators will kick in and block submission. Is there a > way I can add exceptions to the built in validation helpers? Also, > these fields are presently ''not null'' in the database. If I switch > them to allow null will rails automatically allow for the fields to > be blank? Sorry ORML not only adds a new sense of ease, but a new > dimension of complexity for me at the same time :)Add the :allow_nil => true option to your validation declarations. Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDQL4uAQHALep9HFYRAhQ2AJ9OyhmwdDcp0G0Jkwa84tigbZ4kJQCg4624 UI0aGqzUhB3iJqfKoGgZXA8=RqGg -----END PGP SIGNATURE-----
Justin, This still isn''t working for me: validates_format_of :email_address, :with => %r{\b[A-Z0-9._%-]+@[A- Z0-9._%-]+\.[A-Z]{2,4}\b$}i, :message => "is not valid", :allow_nil => true When I submit it with a blank field I still get an error stating the email address is not valid. On Oct 2, 2005, at 9:19 PM, Justin French wrote:> On 03/10/2005, at 11:35 AM, Jim Jeffers wrote: > > > >> I''m using ''validates_format_of'', ''validates_length_of'', and >> ''validates_uniqueness_of'' on certain fields in my DB but these >> fields are not necessarily required. However, if you leave them >> blank the validators will kick in and block submission. Is there >> a way I can add exceptions to the built in validation helpers? >> >> > > You''re thinking too much :) > > http://api.rubyonrails.com/classes/ActiveRecord/Validations/ > ClassMethods.html#M000642 > > validates_length_of() (for example) has an :allow_nil option, which > will skip validation if the attribute is nil. > > Straight from the examples in the docs: > > validates_length_of :fax, :in => 7..32, :allow_nil => true > > There''s also :if which can have a method which can be called to > determine if validation should kick in or not. > > Regards, > > Justin French > --- > TextDrive.com > Indent.com.au > JustinFrench.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
Jim, On 3.10.2005, at 10.49, Jim Jeffers wrote:> Justin, > This still isn''t working for me: > > validates_format_of :email_address, :with => %r{\b[A-Z0-9._%-]+@[A- > Z0-9._%-]+\.[A-Z]{2,4}\b$}i, :message => "is not valid", :allow_nil > => true > > When I submit it with a blank field I still get an error stating > the email address is not valid.The problem is that empty string != nil, no matter how stupid that sounds. This has been discussed before and it might be that the behavior will change in the future. For now, you can edit your regexp to also accept an empty string. //jarkko> > On Oct 2, 2005, at 9:19 PM, Justin French wrote: > > > >> On 03/10/2005, at 11:35 AM, Jim Jeffers wrote: >> >> >> >> >>> I''m using ''validates_format_of'', ''validates_length_of'', and >>> ''validates_uniqueness_of'' on certain fields in my DB but these >>> fields are not necessarily required. However, if you leave them >>> blank the validators will kick in and block submission. Is there >>> a way I can add exceptions to the built in validation helpers? >>> >>> >>> >> >> You''re thinking too much :) >> >> http://api.rubyonrails.com/classes/ActiveRecord/Validations/ >> ClassMethods.html#M000642 >> >> validates_length_of() (for example) has an :allow_nil option, >> which will skip validation if the attribute is nil. >> >> Straight from the examples in the docs: >> >> validates_length_of :fax, :in => 7..32, :allow_nil => true >> >> There''s also :if which can have a method which can be called to >> determine if validation should kick in or not. >> >> Regards, >> >> Justin French >> --- >> TextDrive.com >> Indent.com.au >> JustinFrench.com >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> >> > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks Jarkko, I ended up doing this: validates_format_of :email_address, :with => %r{\b[A-Z0-9._%-]+@[A- Z0-9._%-]+\.[A-Z]{2,4}\b$}i, :message => "is not valid", :if => :email_is_not_blank # Checks to confirm the email address is a blank string. def email_is_not_blank unless self.email_address == "" return true else return false end end Is this the best way to do it or is there a more streamlined way of doing this? OR is there a way I could do something along the lines of: before_validation :convert_blank_fields_to_nil How would I write a function that loops through all of the values and then basically does something like: for column in columns if column == "" column.nil! end end - Jim On Oct 3, 2005, at 12:54 AM, Jarkko Laine wrote:> Jim, > > On 3.10.2005, at 10.49, Jim Jeffers wrote: > > >> Justin, >> This still isn''t working for me: >> >> validates_format_of :email_address, :with => %r{\b[A-Z0-9._%-]+@[A- >> Z0-9._%-]+\.[A-Z]{2,4}\b$}i, :message => "is not >> valid", :allow_nil => true >> >> When I submit it with a blank field I still get an error stating >> the email address is not valid. >> > > The problem is that empty string != nil, no matter how stupid that > sounds. This has been discussed before and it might be that the > behavior will change in the future. For now, you can edit your > regexp to also accept an empty string. > > //jarkko > > >> >> On Oct 2, 2005, at 9:19 PM, Justin French wrote: >> >> >> >> >>> On 03/10/2005, at 11:35 AM, Jim Jeffers wrote: >>> >>> >>> >>> >>> >>>> I''m using ''validates_format_of'', ''validates_length_of'', and >>>> ''validates_uniqueness_of'' on certain fields in my DB but these >>>> fields are not necessarily required. However, if you leave them >>>> blank the validators will kick in and block submission. Is >>>> there a way I can add exceptions to the built in validation >>>> helpers? >>>> >>>> >>>> >>>> >>> >>> You''re thinking too much :) >>> >>> http://api.rubyonrails.com/classes/ActiveRecord/Validations/ >>> ClassMethods.html#M000642 >>> >>> validates_length_of() (for example) has an :allow_nil option, >>> which will skip validation if the attribute is nil. >>> >>> Straight from the examples in the docs: >>> >>> validates_length_of :fax, :in => 7..32, :allow_nil => true >>> >>> There''s also :if which can have a method which can be called to >>> determine if validation should kick in or not. >>> >>> Regards, >>> >>> Justin French >>> --- >>> TextDrive.com >>> Indent.com.au >>> JustinFrench.com >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >>> >>> >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
The convert_blank_to_nil might not be a bad approach. Otherwise, I thought maybe you could just do a general blank? method rather than one specifically named for email, and use that whenever you need to. Question, though... why are you validating fields that don''t require validation? There seems to be a gap in logic there that precedes all this conversation...
On 10/3/05 10:37 AM, "Seth Rasmussen" <seths.mailing.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Question, though... why are you validating fields that don''t require > validation? There seems to be a gap in logic there that precedes all > this conversation...I''ve done this where I allow a user to type in an optional webpage. If they leave it blank, no worries -- if they type something in then I make sure it starts with http:// or https:// Thanks for the note on :allow_nil => true option. That will come in handy some day. ~ Mike -- http://MikeZornek.com
My client does not want to require the users email address. However, if they do enter an email address I do want to ensure that it is unique and that it is properly formatted. - Jim On Oct 3, 2005, at 7:37 AM, Seth Rasmussen wrote:> The convert_blank_to_nil might not be a bad approach. Otherwise, I > thought maybe you could just do a general blank? method rather than > one specifically named for email, and use that whenever you need to. > > Question, though... why are you validating fields that don''t require > validation? There seems to be a gap in logic there that precedes all > this conversation... > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > >
I personally always want to treat empty strings as nil. Here is a good wiki page on how to setup a Whitespace Killer that will do this for all parameters: http://wiki.rubyonrails.org/rails/pages/HowToStripWhitespaceFromModelFields Tom On 10/3/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote:> My client does not want to require the users email address. However, > if they do enter an email address I do want to ensure that it is > unique and that it is properly formatted. > > - Jim > > On Oct 3, 2005, at 7:37 AM, Seth Rasmussen wrote: > > > > > The convert_blank_to_nil might not be a bad approach. Otherwise, I > > thought maybe you could just do a general blank? method rather than > > one specifically named for email, and use that whenever you need to. > > > > Question, though... why are you validating fields that don''t require > > validation? There seems to be a gap in logic there that precedes all > > this conversation... > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Monday 03 Oct 2005 17:37, Jim Jeffers wrote:> My client does not want to require the users email address. However, > if they do enter an email address I do want to ensure that it is > unique and that it is properly formatted.Rather untested here - bodged together from a couple of different parts of my code, but I think this should work for you: validates_uniqueness_of :email_address, :message => "must be unique", :if => Proc.new {|model| !model.email_address.blank?} validates_format_of :email_address, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :message => "is not valid", :if => Proc.new {|model| !model.email_address.blank?} Did that help? ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
Thanks a lot Tom and Dave. Both of these techniques helped me a lot. I was confused as to how the proc.new iterator technique worked at the time. And the observation method is pretty slick as well. - Jim On Oct 3, 2005, at 9:58 AM, Tom Davies wrote:> I personally always want to treat empty strings as nil. Here is a > good wiki page on how to setup a Whitespace Killer that will do this > for all parameters: > > http://wiki.rubyonrails.org/rails/pages/ > HowToStripWhitespaceFromModelFields > > Tom > > On 10/3/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: > >> My client does not want to require the users email address. However, >> if they do enter an email address I do want to ensure that it is >> unique and that it is properly formatted. >> >> - Jim >> >> On Oct 3, 2005, at 7:37 AM, Seth Rasmussen wrote: >> >> >> >> >>> The convert_blank_to_nil might not be a bad approach. Otherwise, I >>> thought maybe you could just do a general blank? method rather than >>> one specifically named for email, and use that whenever you need to. >>> >>> Question, though... why are you validating fields that don''t require >>> validation? There seems to be a gap in logic there that precedes all >>> this conversation... >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >>> >>> >> >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
man, 03,.10.2005 kl. 12.58 -0400, skrev Tom Davies:> I personally always want to treat empty strings as nil. Here is a > good wiki page on how to setup a Whitespace Killer that will do this > for all parameters: > > http://wiki.rubyonrails.org/rails/pages/HowToStripWhitespaceFromModelFields > > TomI have this set up now, but still get "must be a number" for the following: validates_numericality_of :postcode, :allow_nil=>true I instantiate the observer in Environment.rb like so: # Include the Whitespace killer functionality everywhere require File.dirname(__FILE__) + ''/../lib/whitespace_killer'' WhitespaceKiller.instance And the observer itself (located in lib) looks like this: # Converts empty strings to nil! class WhitespaceKiller < ActiveRecord::Observer observe Activity, Contact, Customer, Employee, Entity, ProjectContributor, Project, Supplier, Timesheet 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 The validation in question is being done in the Entity model. It also breaks for validates_uniqueness_of :legalid, :allow_nil => true. Both fields are VARCHAR fields stored in a PostgreSQL database. Help? :) Regards, Henning Pedersen mopo as
Hi Henning, I found that I had to create the instance in my Application controller, like so: class ApplicationController < ActionController::Base WhitespaceKiller.instance It wasn''t loading when I placed it in my environment file for some reason. You can verify when the WhitespaceKiller is loading by writing to the log using: RAILS_DEFAULT_LOGGER.debug(''loading whitespace'') from within your WhitespaceKiller class. Tom On 10/21/05, Henning Kilset Pedersen <henning-m0Gqv1oIyVE@public.gmane.org> wrote:> man, 03,.10.2005 kl. 12.58 -0400, skrev Tom Davies: > > I personally always want to treat empty strings as nil. Here is a > > good wiki page on how to setup a Whitespace Killer that will do this > > for all parameters: > > > > http://wiki.rubyonrails.org/rails/pages/HowToStripWhitespaceFromModelFields > > > > Tom > > I have this set up now, but still get "must be a number" for the > following: > > validates_numericality_of :postcode, :allow_nil=>true > > I instantiate the observer in Environment.rb like so: > > # Include the Whitespace killer functionality everywhere > require File.dirname(__FILE__) + ''/../lib/whitespace_killer'' > WhitespaceKiller.instance > > And the observer itself (located in lib) looks like this: > > # Converts empty strings to nil! > > class WhitespaceKiller < ActiveRecord::Observer > observe Activity, Contact, Customer, Employee, Entity, > ProjectContributor, Project, Supplier, Timesheet > > 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 > > > The validation in question is being done in the Entity model. It also > breaks for validates_uniqueness_of :legalid, :allow_nil => true. > > Both fields are VARCHAR fields stored in a PostgreSQL database. > > Help? :) > > Regards, > Henning Pedersen > mopo as > > > > >