Suppose I have the following in my model: validates_presence_of :zipcode validates_numericality_of :zipcode The resulting page show''s: "Zip can''t be blank" "Zip is not a number" I would like to only show "Zip can''t be blank", and not bombard the user with validation errors when just one would suffice. Is there any way to still validate the numericality if the user provided a String for example... but only show the presence message if they left it blank ? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
validates_peresence_of :zipcode validates_numericality_of :zipcode unless :zipcode.empty? untested code. I did something like this and it worked. -=- Neeraj On 11/3/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Suppose I have the following in my model: > > validates_presence_of :zipcode > validates_numericality_of :zipcode > > The resulting page show''s: > "Zip can''t be blank" > "Zip is not a number" > > I would like to only show "Zip can''t be blank", and not bombard the user > with validation errors when just one would suffice. > Is there any way to still validate the numericality if the user provided a > String for example... but only show the presence message if they left it > blank ? > > _______________________________________________ > 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
Neeraj Kumar wrote:> validates_peresence_of :zipcode > validates_numericality_of :zipcode unless :zipcode.empty? > > untested code. I did something like this and it worked.This won''t work for two reasons: 1) Because this line is run only once when the class is loaded and won''t apply to any of the model''s instance objects. 2) There isn''t an instance method named ''empty?'' for a symbol. Regards, Blair -- Blair Zajac, Ph.D. <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> Subversion and Orca training and consulting http://www.orcaware.com/svn/
true and true... wondering if you could use attribute_present? to do the check ? been messing around with this, but no luck yet. On 11/3/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote:> > Neeraj Kumar wrote: > > validates_peresence_of :zipcode > > validates_numericality_of :zipcode unless :zipcode.empty? > > > > untested code. I did something like this and it worked. > > This won''t work for two reasons: > > 1) Because this line is run only once when the class is loaded and won''t > apply > to any of the model''s instance objects. > > 2) There isn''t an instance method named ''empty?'' for a symbol. > > Regards, > Blair > > -- > Blair Zajac, Ph.D. > <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> > Subversion and Orca training and consulting > http://www.orcaware.com/svn/ > _______________________________________________ > 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
Probably do something based off of this: class Person < ActiveRecord::Base validates_each :first_name, :last_name do |record, attr| record.errors.add attr, ''starts with z.'' if attr[0] == ?z end end Found this on http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html Regards, Blair Dylan Stamat wrote:> true and true... wondering if you could use attribute_present? to do the > check ? > been messing around with this, but no luck yet. > > > On 11/3/05, *Blair Zajac* <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org > <mailto:blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org>> wrote: > > Neeraj Kumar wrote: > > validates_peresence_of :zipcode > > validates_numericality_of :zipcode unless :zipcode.empty? > > > > untested code. I did something like this and it worked. > > This won''t work for two reasons: > > 1) Because this line is run only once when the class is loaded and > won''t apply > to any of the model''s instance objects. > > 2) There isn''t an instance method named ''empty?'' for a symbol. > > Regards, > Blair >
Good find Blair. That is probably the best approach, however, you''d need to dig deeper to actually get the logic implemented. In my example of showing only the "blank" errors if the field is blank, then subsequent errors (like numericality) if the field contains incorrect data... you''d need to do something like this (psuedo-code): class Person < ActiveRecord::Base validates_each :zip, :age do |record, attr| # pull code from record.errors.add_on_blank to get value from attribute, and do an IF block on value.blank?... ie: if !value.blank? # now check numicality, and continue the nested IF''s if more validation needs to occur end end end Not sure if this is the most elegant approach, but all I can come up with at the moment :) On 11/3/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote:> > Probably do something based off of this: > > class Person < ActiveRecord::Base > validates_each :first_name, :last_name do |record, attr| > record.errors.add attr, ''starts with z.'' if attr[0] == ?z > end > end > > Found this on > > > http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html > > Regards, > Blair > > Dylan Stamat wrote: > > true and true... wondering if you could use attribute_present? to do the > > check ? > > been messing around with this, but no luck yet. > > > > > > On 11/3/05, *Blair Zajac* <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org > > <mailto:blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org>> wrote: > > > > Neeraj Kumar wrote: > > > validates_peresence_of :zipcode > > > validates_numericality_of :zipcode unless :zipcode.empty? > > > > > > untested code. I did something like this and it worked. > > > > This won''t work for two reasons: > > > > 1) Because this line is run only once when the class is loaded and > > won''t apply > > to any of the model''s instance objects. > > > > 2) There isn''t an instance method named ''empty?'' for a symbol. > > > > Regards, > > Blair > > > _______________________________________________ > 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 Fri, 2005-04-11 at 10:09 -0800, Dylan Stamat wrote:> Good find Blair. That is probably the best approach, however, you''d > need to dig deeper to actually get the logic implemented. > In my example of showing only the "blank" errors if the field is > blank, then subsequent errors (like numericality) if the field > contains incorrect data... you''d need to do something like this > (psuedo-code): > > class Person < ActiveRecord::Base > validates_each :zip, :age do |record, attr| > # pull code from record.errors.add_on_blank to get value from > attribute, and do an IF block on value.blank?... ie: > if !value.blank? > # now check numicality, and continue the nested IF''s if > more validation needs to occur > end > end > end > > Not sure if this is the most elegant approach, but all I can come up > with at the moment :)A better approach would be a case statement. Same thing, less nesting. class Person < ActiveRecord::Base validates_each :zip, :age do |record, attr| case when value.blank?: # return error msg when !(value.kind_of? Integer): # return error msg default: # all validation passed end end end - Jamie> > On 11/3/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote: > Probably do something based off of this: > > class Person < ActiveRecord::Base > validates_each :first_name, :last_name do |record, attr| > record.errors.add attr, ''starts with z.'' if attr[0] == ?z > end > end > > Found this on > > http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html > > Regards, > Blair > > Dylan Stamat wrote: > > true and true... wondering if you could use > attribute_present? to do the > > check ? > > been messing around with this, but no luck yet. > > > > > > On 11/3/05, *Blair Zajac* < blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org > > <mailto:blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org>> wrote: > > > > Neeraj Kumar wrote: > > > validates_peresence_of :zipcode > > > validates_numericality_of :zipcode > unless :zipcode.empty? > > > > > > untested code. I did something like this and it > worked. > > > > This won''t work for two reasons: > > > > 1) Because this line is run only once when the class is > loaded and > > won''t apply > > to any of the model''s instance objects. > > > > 2) There isn''t an instance method named ''empty?'' for a > symbol. > > > > Regards, > > Blair > > > _______________________________________________ > 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
Thank Jaime, good point. Excuse my ignorance, but what is the best technique to get the value of the "attr" attribute in this scope ? On 11/4/05, Jamie Macey <jamie-7g3wz9A/6AxWk0Htik3J/w@public.gmane.org> wrote:> > On Fri, 2005-04-11 at 10:09 -0800, Dylan Stamat wrote: > > Good find Blair. That is probably the best approach, however, you''d > > need to dig deeper to actually get the logic implemented. > > In my example of showing only the "blank" errors if the field is > > blank, then subsequent errors (like numericality) if the field > > contains incorrect data... you''d need to do something like this > > (psuedo-code): > > > > class Person < ActiveRecord::Base > > validates_each :zip, :age do |record, attr| > > # pull code from record.errors.add_on_blank to get value from > > attribute, and do an IF block on value.blank?... ie: > > if !value.blank? > > # now check numicality, and continue the nested IF''s if > > more validation needs to occur > > end > > end > > end > > > > Not sure if this is the most elegant approach, but all I can come up > > with at the moment :) > > A better approach would be a case statement. Same thing, less nesting. > > class Person < ActiveRecord::Base > validates_each :zip, :age do |record, attr| > case > when value.blank?: # return error msg > when !(value.kind_of? Integer): # return error msg > default: # all validation passed > end > end > end > > - Jamie > > > > > On 11/3/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote: > > Probably do something based off of this: > > > > class Person < ActiveRecord::Base > > validates_each :first_name, :last_name do |record, attr| > > record.errors.add attr, ''starts with z.'' if attr[0] == ?z > > end > > end > > > > Found this on > > > > > http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html > > > > Regards, > > Blair > > > > Dylan Stamat wrote: > > > true and true... wondering if you could use > > attribute_present? to do the > > > check ? > > > been messing around with this, but no luck yet. > > > > > > > > > On 11/3/05, *Blair Zajac* < blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org > > > <mailto:blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org>> wrote: > > > > > > Neeraj Kumar wrote: > > > > validates_peresence_of :zipcode > > > > validates_numericality_of :zipcode > > unless :zipcode.empty? > > > > > > > > untested code. I did something like this and it > > worked. > > > > > > This won''t work for two reasons: > > > > > > 1) Because this line is run only once when the class is > > loaded and > > > won''t apply > > > to any of the model''s instance objects. > > > > > > 2) There isn''t an instance method named ''empty?'' for a > > symbol. > > > > > > Regards, > > > Blair > > > > > _______________________________________________ > > 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 >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 11/3/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Suppose I have the following in my model: > > validates_presence_of :zipcode > validates_numericality_of :zipcodePlease don''t do such things... canada for example has letters in its zipcode and there are many countries which have zip codes starting with zero ( which are not correct unless that zero stays there ). zipcode is a string not a number. -- Tobi http://jadedpixel.com - modern e-commerce software http://typo.leetsoft.com - Open source weblog engine http://blog.leetsoft.com - Technical weblog
Understood Tobi... will keep it a String. Question: How do I access the attributes values in the "validate_each" ? I can reference "attr", but don''t know of a way to get the value from the generic attribute. Any ideas ? On 11/4/05, Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 11/3/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Suppose I have the following in my model: > > > > validates_presence_of :zipcode > > validates_numericality_of :zipcode > > Please don''t do such things... canada for example has letters in its > zipcode and there are many countries which have zip codes starting > with zero ( which are not correct unless that zero stays there ). > zipcode is a string not a number. > > -- > Tobi > http://jadedpixel.com - modern e-commerce software > http://typo.leetsoft.com - Open source weblog engine > http://blog.leetsoft.com - Technical weblog > _______________________________________________ > 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
Whoaaaa... nevermind me. Just realized the "validate_each" method can iterate over |record, attr, value| ... I didn''t see the value parameter before. I need to RTFM more. Sorry folks... move along now... nothing to see here :) On 11/7/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Understood Tobi... will keep it a String. > > Question: How do I access the attributes values in the "validate_each" ? > I can reference "attr", but don''t know of a way to get the value from the > generic attribute. > > Any ideas ? > > > On 11/4/05, Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On 11/3/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Suppose I have the following in my model: > > > > > > validates_presence_of :zipcode > > > validates_numericality_of :zipcode > > > > Please don''t do such things... canada for example has letters in its > > zipcode and there are many countries which have zip codes starting > > with zero ( which are not correct unless that zero stays there ). > > zipcode is a string not a number. > > > > -- > > Tobi > > http://jadedpixel.com - modern e-commerce software > > http://typo.leetsoft.com - Open source weblog engine > > http://blog.leetsoft.com - Technical weblog > > _______________________________________________ > > 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