Created
September 4, 2012 13:25
-
-
Save ankit8898/3621130 to your computer and use it in GitHub Desktop.
Custom date validation to prevent the save of default date in Rails Activerecord with validates_with
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Place this file in your config/initializers | |
class CheckDate < ActiveModel::Validator | |
#Sat Jan 01 00:00:00 UTC 2000 is the default date that rails store | |
def validate(record) | |
if record.start_time == "Sat Jan 01 00:00:00 UTC 2000".to_date | |
record.errors[:base] << "This is a invalid date" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Schema entry | |
#t.time "start_time", :null => false | |
#t.datetime "created_at" | |
#t.datetime "updated_at" | |
class MyModel < ActiveRecord::Base | |
validates_with CheckDate | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment