<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>I.NFECTIO.US: Handling Invalid Dates with ActiveRecord Date Helpers</title>
    <link>http://i.nfectio.us/articles/2006/02/22/handling-invalid-dates-with-activerecord-date-helpers</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>"Nothing in life is worth doing if you have no life while doing it"</description>
    <item>
      <title>Handling Invalid Dates with ActiveRecord Date Helpers</title>
      <description>If you've worked with the model based date helpers in Rails (date_select and datetime_select), then you may know that selecting an invalid date throws an exception deep within ActiveRecord. A lot of users assume they can handle this in the model validation, but due to the way ActiveRecord handles multi-parameter input values, it will raise an exception if an invalid date is input. Here's some sample code of how you can catch and handle this exception without affecting the user experience too much.
&lt;div style="margin:10px;border: 1px solid #8DBA47; width:500px; height:250px; overflow:scroll; background-color: #F1F8E4;"&gt;
&lt;pre&gt;
In controller.rb:

def create
  begin
    @client = Client.new(params[:client])
  # Catch the exception from AR::Base
  rescue ActiveRecord::MultiparameterAssignmentErrors =&gt; ex
    # Iterarate over the exceptions and remove the invalid field components from the input
    ex.errors.each { |err| params[:client].delete_if { |key, value| key =~ /^#{err.attribute}/ } }
    # Recreate the Model with the bad input fields removed
    @client = Client.new(params[:client])      
  end
    
  if @client.save
    flash[:notice] = 'Client was successfully created.'
    redirect_to :action =&gt; 'list'
  else
    render :action =&gt; 'new'
  end
end

In model.rb:

validates_presence_of :date_field_name, :message =&gt; 'must be a valid date'

&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 22 Feb 2006 19:42:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:c8d5414be1f046ef3a5639c0ea0deae2</guid>
      <author>Bob Silva</author>
      <link>http://i.nfectio.us/articles/2006/02/22/handling-invalid-dates-with-activerecord-date-helpers</link>
      <category>Ruby on Rails</category>
      <category>rails</category>
      <category>activerecord</category>
      <category>dates</category>
    </item>
    <item>
      <title>"Handling Invalid Dates with ActiveRecord Date Helpers" by topfunky</title>
      <description>There is also a plugin that moves these errors into the regular errors hash:

&lt;a href="http://wiki.rubyonrails.org/rails/pages/Validates+Multiparameter+Assignments" rel="nofollow"&gt;http://wiki.rubyonrails.org/rails/pages/Validates+Multiparameter+Assignments&lt;/a&gt;
</description>
      <pubDate>Mon, 27 Nov 2006 12:49:33 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:2ccf8f69-f2c1-4cad-b1e6-6bebd37c75af</guid>
      <link>http://i.nfectio.us/articles/2006/02/22/handling-invalid-dates-with-activerecord-date-helpers#comment-226</link>
    </item>
    <item>
      <title>"Handling Invalid Dates with ActiveRecord Date Helpers" by mek</title>
      <description>there is always a good solution in RAILS! :D</description>
      <pubDate>Thu, 14 Sep 2006 07:54:56 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:2c8b92c1-1055-4562-af93-726f01926c98</guid>
      <link>http://i.nfectio.us/articles/2006/02/22/handling-invalid-dates-with-activerecord-date-helpers#comment-135</link>
    </item>
    <item>
      <title>"Handling Invalid Dates with ActiveRecord Date Helpers" by Paul</title>
      <description>Thanks for the solution, works great.</description>
      <pubDate>Sun, 30 Jul 2006 21:15:46 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:93fa7a50-5296-46b3-a918-bd85001888fa</guid>
      <link>http://i.nfectio.us/articles/2006/02/22/handling-invalid-dates-with-activerecord-date-helpers#comment-38</link>
    </item>
  </channel>
</rss>
