Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A rails plugin to validate date time attributes which also supports setting time values via strings.
Ruby
Branch: master
Pull request Compare This branch is 1 commit ahead, 1 commit behind robertwahler:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
MIT-LICENSE
README.rdoc
Rakefile
init.rb
validates_as_time.gemspec

README.rdoc

validates_as_time

A rails plugin to validate date time attributes which also supports setting time values via strings.

This plugin helps with writing minimal code to add date time attributes to your models that are parsed using strings. The strings are parsed using the Time.parse method, or if you have required chronic in your code it will use Chronic.parse for all kinds of extra niceness. For more information on chronic see chronic.rubyforge.org.

In addition this plugin can be used to simply validate date time attributes with the usual options like :minimum, :maximum, :allow_nil, etc.

Install

If you want a static version of this plugin you can download the tarball from:

http://github.com/lawrencepit/validates_as_time/tarball/master

or if you have git installed:

cd myapp
git clone git://github.com/lawrencepit/validates_as_time.git vendor/plugins/validates_as_time
rm -Rf vendor/plugins/validates_as_time/.git

Or add as a submodule:

git submodule add git://github.com/lawrencepit/validates_as_time.git vendor/plugins/validates_as_time

Usage

For example:

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

This also creates accessor methods named starts_at_string and ends_at_string.

and in your view:

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

You can also provide extra configuration parameters like you are used to with the standard rails validates methods. For example:

validates_as_time :starts_at, :message => "could not be parsed", 
                  :allow_nil => false, :if => Proc.new { |user| user.signup_step > 2 })

or:

validates_as_time :due_at, :minimum => Time.now, :maximum => "1 jan 2009", 
                  :too_early => "You cannot create a task that is due before the current time", 
                  :too_late => "You cannot create a task that is due next year"

Configuration options

  • :default - The default value to use when the attribute is empty. This default is only used to present the string value of the time attribute. Default is Time.now

  • :format - The format used to present a Time object. Default is “%Y-%m-%d %H:%M”. See the method Time.strftime for more formatting options.

  • :minimum - The minimum value allowed for the attribute. You can provide a Time object or a string which will be parsed by Chronic.parse.

  • :maximum - The maximum value allowed for the attribute. You can provide a Time object or a string which will be parsed by Chronic.parse.

  • :message - A custom error message (default is: “is invalid”)

  • :too_early - A custom error message when the time falls before the :minimum given (default is: “cannot be before %s”).

  • :too_late - A custom error message when the time falls on or after the :maximum given (default is: “cannot be on or after %s”).

  • :allow_nil - Skip validation if attribute is nil (default is: true).

  • :if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.

  • :unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }).

  • :on - Specifies when this validation is active (default is :save, other options :create, :update)

  • :preparser - Specifies a method or proc to call with the string value that was entered. The return value is used as the argument to the parse method of Time.

  • :parser - Use the given Class as parser

You could override these options globally by modifying the following hash:

ValidatesAsTime.default_options

Localization

To localize the default messages, override:

ActiveRecord::Errors.default_error_messages[:invalid]
ActiveRecord::Errors.default_error_messages[:blank]
ValidatesAsTime.default_options[:too_early]
ValidatesAsTime.default_options[:too_late]

The messages :too_early and :too_late can accept a placeholder %s which will be replaced with the given minimum resp. maximum value.

Author

Lawrence Pit (lawrence.pit@gmail.com)

Something went wrong with that request. Please try again.