I''m hoping someone can help me (a Rails newbie) with this. Basically, I need to verify that a date created with "date_select" is valid *before* it''s sent off to the database for acceptance (as a valid date) or rejection (as an invalid date). Given how easy the majority of validation are in Rails, I''ve got to believe there''s a fairly simple way to do this, but for the life of me I can''t find it. At this point, I''m even ready to accept a difficult solution, as long as it''s a solution. Any ideas would be greatly appreciated. -- Nathan Wright Rocky Mountain Harley-Davidson
I''m hoping someone can help me (a Rails newbie) with this. Basically, I need to verify that a date created with "date_select" is valid *before* it''s sent off to the database for acceptance (as a valid date) or rejection (as an invalid date). Given how easy the majority of validation are in Rails, I''ve got to believe there''s a fairly simple way to do this, but for the life of me I can''t find it. At this point, I''m even ready to accept a difficult solution, as long as it''s a solution. Any ideas would be greatly appreciated. -- Nathan Wright
On Wed, 02 Mar 2005 12:11:46 -0700, Nathan Wright <nathan-BJus4bFyWF4AvxtiuMwx3w@public.gmane.org> wrote:> I''m hoping someone can help me (a Rails newbie) with this. Basically, I > need to verify that a date created with "date_select" is valid *before* > it''s sent off to the database for acceptance (as a valid date) or > rejection (as an invalid date). Given how easy the majority of validation > are in Rails, I''ve got to believe there''s a fairly simple way to do this, > but for the life of me I can''t find it.What do you mean ''valid'' in this context? Is it some business rule validation (user must be over 18) or some formatting problem?> At this point, I''m even ready to accept a difficult solution, as long as > it''s a solution. Any ideas would be greatly appreciated. > > -- > Nathan Wright > Rocky Mountain Harley-Davidson > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
On Wed, Mar 02, 2005 at 12:11:46PM -0700, Nathan Wright wrote:> I''m hoping someone can help me (a Rails newbie) with this. Basically, I > need to verify that a date created with "date_select" is valid *before* > it''s sent off to the database for acceptance (as a valid date) or > rejection (as an invalid date). Given how easy the majority of validation > are in Rails, I''ve got to believe there''s a fairly simple way to do this, > but for the life of me I can''t find it. > > At this point, I''m even ready to accept a difficult solution, as long as > it''s a solution. Any ideas would be greatly appreciated.There is a method in AR::Base called execute_callstack_for_multiparameter_attributes. It has a line that reads: send(name + "=", Time == klass ? klass.local(*values) : klass.new(*values)) If someone submits February 30 in a form helper then Time.local gets passed an invalid date which makes it raise an exception. This happens before the validation callbacks so you can not protect yourself from it, nor do I even think there is a way from outside of AR itself to wrap this in an exception. Though I would concede to whomever would be of the position that an invalid date is an exceptional state, I''d rather be able to catch this in AR validations and report it to the user then have my app bomb out. I set out to patch this a while ago but didn''t come up with anything satisfactory because of where this method is defined and how it is called. Granted I only spent a few hours on it. I would be psyched if someone made this something that validations can catch. marcel -- Marcel Molina Jr. <marcel-WRrfy3IlpWYdnm+yROfE0A@public.gmane.org>
Marcel Molina Jr. wrote:> On Wed, Mar 02, 2005 at 12:11:46PM -0700, Nathan Wright wrote: > >>I''m hoping someone can help me (a Rails newbie) with this. Basically, I >>need to verify that a date created with "date_select" is valid *before* >>it''s sent off to the database for acceptance (as a valid date) or >>rejection (as an invalid date). Given how easy the majority of validation >>are in Rails, I''ve got to believe there''s a fairly simple way to do this, >>but for the life of me I can''t find it. >> >>At this point, I''m even ready to accept a difficult solution, as long as >>it''s a solution. Any ideas would be greatly appreciated. > > > There is a method in AR::Base called > execute_callstack_for_multiparameter_attributes. It has a line that reads: > > send(name + "=", Time == klass ? klass.local(*values) : klass.new(*values)) > > If someone submits February 30 in a form helper then Time.local gets > passed an invalid date which makes it raise an exception. This happens > before the validation callbacks so you can not protect yourself from it, > nor do I even think there is a way from outside of AR itself to wrap > this in an exception. > > Though I would concede to whomever would be of the position that an > invalid date is an exceptional state, I''d rather be able to catch this in > AR validations and report it to the user then have my app bomb out. I > set out to patch this a while ago but didn''t come up with anything > satisfactory because of where this method is defined and how it is > called. Granted I only spent a few hours on it. I would be psyched if > someone made this something that validations can catch. > > marcelDearest Marcel, I agree. Would you care to send a patch? My utmost thanks, Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Marcel Molina Jr. wrote: [snip]> There is a method in AR::Base called > execute_callstack_for_multiparameter_attributes. It has a line that reads: > > send(name + "=", Time == klass ? klass.local(*values) : klass.new(*values)) > > If someone submits February 30 in a form helper then Time.local gets > passed an invalid date which makes it raise an exception. This happens > before the validation callbacks so you can not protect yourself from it, > nor do I even think there is a way from outside of AR itself to wrap > this in an exception.[snip] You can wrap the .save / .update in a begin ... rescue ... end in the controller. It does work, but it isn''t elegant. A helper to test for valid dates would be useful... John
> What do you mean ''valid'' in this context? Is it some business rule > validation (user must be over 18) or some formatting problem?Basically I mean that the date is an actual date -- say February 28, 2005 -- and not a "made up" date -- February 29, 2005. -- Nathan Wright http://www.brandalism.com
> If someone submits February 30 in a form helper then Time.local gets > passed an invalid date which makes it raise an exception. This happens > before the validation callbacks so you can not protect yourself from it, > nor do I even think there is a way from outside of AR itself to wrap > this in an exception.Glad to see that I''m not the only one banging my head against a wall on this one. The Date2 library found at <URL: http://www.funaba.org/en/ruby.html > has a "valid_date?" method that tests for date validity, but I''m having two problems using it: 1. I can''t figure out how to successfully call a new library from within Rails 2. Even if I can call the new library (the brutish way, by replacing the stock "Date" library in Ruby entirely), I still can''t call the "valid_date?" method prior to the data being sent to the database.> I would be psyched if > someone made this something that validations can catch.Amen to that. :) -- Nathan Wright http://www.brandalism.com
> I''m hoping someone can help me (a Rails newbie) with this. Basically, I > need to verify that a date created with "date_select" is valid *before* > it''s sent off to the database for acceptance..You can certainly rescue the error before the app bombs, although this won''t necessarily help you validate in the first place. From John''s new tutorial at http://rails.homelinux.org/ some simple code for this exact error (the "February 31st problem"): def create begin @item = Item.new(@params[''item'']) if @item.save flash[''notice''] = ''Item was successfully created.'' redirect_to :action => ''list_by_priority'' else @categories = Category.find_all render_action ''new'' end rescue flash[''notice''] = ''Item could not be saved.'' redirect_to :action => ''new'' end end More on Ruby exceptions at: http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html
> You can certainly rescue the error before the app bombs, although this > won''t necessarily help you validate in the first place. From John''s new > tutorial at http://rails.homelinux.org/ some simple code for this exact > error (the "February 31st problem"):While it will definitely capture the error, by redirecting to :action => "new" I believe that the user (upon arriving at new.rhtml) would see that he has lost all the information that he entered into the form. Not as graceful as I had hoped. -- Nathan Wright http://www.brandalism.com