Joshua Muheim
2007-Oct-16 20:55 UTC
validates_... :if option: only validate when not empty?
Hi all I have several fields that are optional in a web form. E.g. the user may enter his age or not. Sadly the validates_numericality_of(:age) always fires, also when the user omitted the field. So no entry results in a error message. What''s the easiest way to fix this? I tried validates_numericality_of :age, :if => Proc.new { |m| !m.age.empty? } But somehow this just doesn''t seem to do anything?! The coolest solution would be if I could only specify :if => :not_empty? Maybe there''s a plugin or something to achieve this behavior? :-) I could write a method not_empty() myself: def not_empty? age.empty? end But what if I wanted to use the not_empty? method for other attributes, too? validates_numericality_of :age, :if => :not_empty? validates_numericality_of :height_in_inches, :if => :not_empty? Then I would need some way to determine which attribute is meant in the not_empty? method. But I have no idea how to achieve this... :-/ So I guess I can''t just define a new method not_empty?() but have to overwrite some existing code somehow, so it reacts special when :not_empty? is delivered as :if option...? Please give me some help how to do this or if there are even better solutions for this. :-) Thanks a lot for help! :-) Josh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
tharrison
2007-Oct-17 01:00 UTC
Re: validates_... :if option: only validate when not empty?
On Oct 16, 4:55 pm, Joshua Muheim <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all > > I have several fields that are optional in a web form. E.g. the user may > enter his age or not. > > Sadly the validates_numericality_of(:age) always fires, also when the > user omitted the field. So no entry results in a error message. > What''s the easiest way to fix this? > > I tried > > validates_numericality_of :age, > :if => Proc.new { |m| !m.age.empty? } > > But somehow this just doesn''t seem to do anything?!... This basic structure should work. When the value of the :if condition is false, the validation will not run. So perhaps it''s the "empty?" method -- should it be "blank?" instead. I tried to find any reference to the differences between blank? empty? nil?. Looking further in the book, it seems that blank? is true if the receiver is nil or an empty string. And there''s a thread I found on this group: see http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/12eca4adcddcea9d/b33248819234726f?lnk=gst&q=.empty#b33248819234726f I had trouble finding "blank" documented in the API for ruby or rails, but this could just be because I wasn''t looking in the right place :-) Tom --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Muheim
2007-Oct-17 08:10 UTC
Re: validates_... :if option: only validate when not empty?
Thank you! It works now with the Proc. However, when doing the same with my own validation method it does not work: validates_url_format_of :url, :if => Proc.new { |m| !m.url.empty? } ActiveRecord::Errors::default_error_messages[:invalid_url_protocol] = ''must be using the HTTP or HTTPS protocoll'' ActiveRecord::Errors::default_error_messages[:invalid_url_format] = ''has an incorrect link format'' module ActiveRecord::Validations::ClassMethods def validates_url_format_of(*attr_names) validates_each(attr_names.first.to_s) do |record, attr_name, value| record.send("#{attr_name}=".to_sym, ''http://'' + record.send(attr_name.to_sym)) if !record.send(attr_name.to_sym).empty? and record.send(attr_name.to_sym) !~ /^[a-z]+:\/\// if record.send(attr_name.to_sym) !~ /^(http|https):\/\//i record.errors.add(attr_name, ActiveRecord::Errors::default_error_messages[:invalid_url_protocol]) elsif record.send(attr_name.to_sym) !~ /^(http|https):\/\/([a-z0-9\-äöüèàéê\.]+)(\.[a-z]{2,})(\/(.*))?$/i record.errors.add(attr_name, ActiveRecord::Errors::default_error_messages[:invalid_url_format]) end end end def validates_email_format_of(*attr_names) # TODO: implement! end end What do I have to implement that the :if option works? (But anyway, the :if => :not_empty? would be cooler...) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Franz Strebel
2007-Oct-17 08:20 UTC
Re: validates_... :if option: only validate when not empty?
hmm, just use the option :allow_nil => true --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Oct-17 08:23 UTC
Re: validates_... :if option: only validate when not empty?
On Oct 16, 2007, at 9:00 PM, tharrison wrote:> > On Oct 16, 4:55 pm, Joshua Muheim <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> Hi all >> >> I have several fields that are optional in a web form. E.g. the >> user may >> enter his age or not. >> >> Sadly the validates_numericality_of(:age) always fires, also when the >> user omitted the field. So no entry results in a error message. >> What''s the easiest way to fix this? >> >> I tried >> >> validates_numericality_of :age, >> :if => Proc.new { |m| !m.age.empty? } >> >> But somehow this just doesn''t seem to do anything?! > ... > > This basic structure should work. When the value of the :if condition > is false, the validation will not run. So perhaps it''s the "empty?" > method -- should it be "blank?" instead. I tried to find any > reference to the differences between blank? empty? nil?. Looking > further in the book, it seems that blank? is true if the receiver is > nil or an empty string. And there''s a thread I found on this group: > see http://groups.google.com/group/rubyonrails-talk/browse_frm/ > thread/12eca4adcddcea9d/b33248819234726f? > lnk=gst&q=.empty#b33248819234726f > > I had trouble finding "blank" documented in the API for ruby or rails, > but this could just be because I wasn''t looking in the right place :-) > > TomRails (ActiveSupport) defines blank? but doesn''t document it (the code has #:nodoc: tags on the methods to keep rdoc from including it). vendor/rails/activesupport/lib/active_support/core_ext/blank.rb -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Muheim
2007-Oct-17 08:41 UTC
Re: validates_... :if option: only validate when not empty?
Franz Strebel wrote:> hmm, just use the option > > :allow_nil => trueOh my god, didn''t see the forest because of all those trees! ;-) Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---