From 2f3efa2107676537b6e6ed8671ce8a2f7920ae93 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Tue, 31 Aug 2010 13:23:07 +1000 Subject: [PATCH] custom error message options --- README.rdoc | 26 +++++++++++++++++++++ lib/validates_timeliness/validator.rb | 2 +- spec/validates_timeliness/validator_spec.rb | 9 +++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 4778a8f..ab3d43a 100644 --- a/README.rdoc +++ b/README.rdoc @@ -90,6 +90,17 @@ Regular validation options: :if - Execute validation when :if evaluates true :unless - Execute validation when :unless evaluates false +The temporal restrictions can take 4 different value types: + +* String value +* Date, Time, or DateTime object value +* Proc or lambda object which may take an optional parameter, being the record object +* A symbol matching a method name in the model + +When an attribute value is compared to temporal restrictions, they are compared as +the same type as the validation method type. So using validates_date means all +values are compared as dates. + == Configuration @@ -111,6 +122,21 @@ Using the I18n system to define new defaults: The %{restriction} signifies where the interpolation value for the restriction will be inserted. +You can also use validation options for custom error messages. The following option keys are available: + + :invalid_date_message + :invalid_time_message + :invalid_datetime_message + :is_at_message + :before_message + :on_or_before_message + :after_message + :on_or_after_message + + * There is no :between_message option. The between error message should be defined using the :on_or_before and :on_or_after messages. + +However, it is highly recommended you use the I18n system for error messages. + === Restriction Option Shorthand diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 2ad3478..ec13d20 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -45,7 +45,7 @@ module ValidatesTimeliness begin restriction_value = type_cast(evaluate_option_value(options[restriction], record)) unless value.send(RESTRICTIONS[restriction], restriction_value) - return record.errors.add(attr_name, restriction, :restriction => format_error_value(restriction_value)) + return record.errors.add(attr_name, restriction, :message => options[:"#{restriction}_message"], :restriction => format_error_value(restriction_value)) end rescue => e unless ValidatesTimeliness.ignore_restriction_errors diff --git a/spec/validates_timeliness/validator_spec.rb b/spec/validates_timeliness/validator_spec.rb index 0fb4c97..650f159 100644 --- a/spec/validates_timeliness/validator_spec.rb +++ b/spec/validates_timeliness/validator_spec.rb @@ -130,4 +130,13 @@ describe ValidatesTimeliness::Validator do end end end + + context "custom error message" do + + it 'should be used for failing restriction' do + Person.validates_date :birth_date, :before => Time.now, :before_message => 'custom before message' + invalid!(:birth_date, Time.now, 'custom before message') + end + + end end