So, here''s a case something didn''t work as expected, and I''m wondering if a feature tweak is worth considering. If I use :allow_nil => TRUE, validation rules still complain if a form field is submitted empty (w/o using a presence_of rule). So... validates_format_of :first_name, :with => Is_human_name, :allow_nil => TRUE, :message => Is_not_human_name_msg Will complain if the field is submitted as empty. Now, empty doesn''t conform to the regex I am using, but I was expecting that :allow_nil will cause the validation to be ignored when the value is empty. After thinking about it, I can see that params creates emptry strings which are not interpreted as nil for saving. That makes sense, and therefore I can see why Rails still validated my format rule. Ultimately what I was trying to do was have certain messages override the need for others. If I have this: validates_presence_of :first_name validates_format_of :first_name, :with => Is_human_name, :allow_nil => TRUE, :message => Is_not_human_name_msg And the field is submitted as empty, then the only error message I need to see is the one that says the field cannot be empty. I do not need to see the format_of error message, or another any subsequent rule failure that might apply. It was suggested that using an :if is the typical strategy for accomplishing that: :if => Proc.new { |model_name| model_name.first_name.length > 0 } That seems rather bulky (especially when repetitive), and I wonder if an :allow_empty => TRUE couldn''t be used to provide the behavior I was expecting -- rules are ignored for *empty* fields, not just Nil ones. Or perhaps it is better named, :ignore_empty => TRUE Maybe this is too small a use case, but it sure reduces the :if bulk. Just a thought... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
:allow_blank is in 2.0 AFAIK, so that should do what you want On 11/28/07, Greg Willits <ruby-forum-incoming@andreas-s.net> wrote:> > So, here''s a case something didn''t work as expected, and I''m wondering > if a feature tweak is worth considering. > > If I use :allow_nil => TRUE, validation rules still complain if a form > field is submitted empty (w/o using a presence_of rule). > > So... > > validates_format_of :first_name, :with => Is_human_name, > :allow_nil => TRUE, :message => Is_not_human_name_msg > > Will complain if the field is submitted as empty. Now, empty doesn''t > conform to the regex I am using, but I was expecting that :allow_nil > will cause the validation to be ignored when the value is empty. > > After thinking about it, I can see that params creates emptry strings > which are not interpreted as nil for saving. That makes sense, and > therefore I can see why Rails still validated my format rule. > > Ultimately what I was trying to do was have certain messages override > the need for others. > > If I have this: > > validates_presence_of :first_name > > validates_format_of :first_name, :with => Is_human_name, > :allow_nil => TRUE, :message => Is_not_human_name_msg > > And the field is submitted as empty, then the only error message I need > to see is the one that says the field cannot be empty. I do not need to > see the format_of error message, or another any subsequent rule failure > that might apply. > > It was suggested that using an :if is the typical strategy for > accomplishing that: > > :if => Proc.new { |model_name| model_name.first_name.length > 0 } > > That seems rather bulky (especially when repetitive), and I wonder if an > :allow_empty => TRUE couldn''t be used to provide the behavior I was > expecting -- rules are ignored for *empty* fields, not just Nil ones. > > Or perhaps it is better named, :ignore_empty => TRUE > > Maybe this is too small a use case, but it sure reduces the :if bulk. > Just a thought... > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Kaspick wrote:> :allow_blank is in 2.0 AFAIK, so that should do what you wantAh. Yep. a search for allow_blank reveals http://dev.rubyonrails.org/ticket/7383 Didn''t catch that with my other searches. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
If I use :allow_blank => false I expect that no blanks are allowed, however, they are allowed. def test_validates_length_of_with_no_allow_blank Topic.validates_length_of( :title, :maximum => 5, :allow_blank=>false ) assert !Topic.create("title" => "abcdefg").valid? assert !Topic.create("title" => "").valid? assert !Topic.create("title" => nil).valid? assert Topic.create("title" => "abcde").valid? end This test fails on the 2nd assert. The default validation options include :allow_nil=>false and :allow_blank=>false. I suggest those are removed, and only when those options are explicitely passed into the options hash of one of the validate methods would you check for allow_nil and/or allow_blank. Futhermore, now that we have the :allow_blank option, does anyone understand why validate_presence_of can''t use validates_each? And why does that method need to be able to deal with "nonexistent attributes" and all the other validate methods not ?? Cheers Lawrence> :allow_blank is in 2.0 AFAIK, so that should do what you want > > On 11/28/07, Greg Willits <ruby-forum-incoming@andreas-s.net> wrote: > >> So, here''s a case something didn''t work as expected, and I''m wondering >> if a feature tweak is worth considering. >> >> If I use :allow_nil => TRUE, validation rules still complain if a form >> field is submitted empty (w/o using a presence_of rule). >> >> So... >> >> validates_format_of :first_name, :with => Is_human_name, >> :allow_nil => TRUE, :message => Is_not_human_name_msg >> >> Will complain if the field is submitted as empty. Now, empty doesn''t >> conform to the regex I am using, but I was expecting that :allow_nil >> will cause the validation to be ignored when the value is empty. >> >> After thinking about it, I can see that params creates emptry strings >> which are not interpreted as nil for saving. That makes sense, and >> therefore I can see why Rails still validated my format rule. >> >> Ultimately what I was trying to do was have certain messages override >> the need for others. >> >> If I have this: >> >> validates_presence_of :first_name >> >> validates_format_of :first_name, :with => Is_human_name, >> :allow_nil => TRUE, :message => Is_not_human_name_msg >> >> And the field is submitted as empty, then the only error message I need >> to see is the one that says the field cannot be empty. I do not need to >> see the format_of error message, or another any subsequent rule failure >> that might apply. >> >> It was suggested that using an :if is the typical strategy for >> accomplishing that: >> >> :if => Proc.new { |model_name| model_name.first_name.length > 0 } >> >> That seems rather bulky (especially when repetitive), and I wonder if an >> :allow_empty => TRUE couldn''t be used to provide the behavior I was >> expecting -- rules are ignored for *empty* fields, not just Nil ones. >> >> Or perhaps it is better named, :ignore_empty => TRUE >> >> Maybe this is too small a use case, but it sure reduces the :if bulk. >> Just a thought... >> -- >> Posted via http://www.ruby-forum.com/. >> >> > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---