custom error message options

This commit is contained in:
Adam Meehan 2010-08-31 13:23:07 +10:00
parent 728439201d
commit 2f3efa2107
3 changed files with 36 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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