Rails plugin validates_as_time

May 19, 2008

Exactly one year ago Railscast episode 32 was released. Watch the episode to see Ryan Bates explaining how to use a virtual attribute in your rails models to allow editing time attributes with text fields in a more flexible way.

Based on this I decided to make it even more flexible. The result is the rails plugin validates_as_time. It supports the fabulous Chronic gem automatically if you have it installed, otherwise it falls back to using Time.parse as the method to parse time string values.

In it's simplest form just define the time fields you want to validate with string values:

class Project < ActiveRecord::Base
  validates_as_time :starts_at, :ends_at
end

and in your view:

<%= f.text_field :starts_at_string %>
<%= f.text_field :ends_at_string %>

You can clone/fork the source via:
http://github.com/lawrencepit/validates_as_time


Comments

No comments yet.

Leave a comment