Skip to content

Instantly share code, notes, and snippets.

@chrisyour
Created March 8, 2011 19:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Simple date validator for Rails 3.
# Validator:
# /app/validators/date_validator.rb
class DateValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value.to_s =~ /^\d{4}-\d{2}-\d{2}$/i
record.errors[attribute] << (options[:message] || "is an invalid date")
end
end
end
# Use:
# /app/models/post.rb
class Post < ActiveRecord::Base
validates :published_on, :date => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment