[Rails] date validation

Andrew Otwell andrew at heyotwell.com
Wed Mar 2 21:02:04 GMT 2005


> 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



More information about the Rails mailing list