mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
custom error message options
This commit is contained in:
parent
728439201d
commit
2f3efa2107
26
README.rdoc
26
README.rdoc
@ -90,6 +90,17 @@ Regular validation options:
|
|||||||
:if - Execute validation when :if evaluates true
|
:if - Execute validation when :if evaluates true
|
||||||
:unless - Execute validation when :unless evaluates false
|
: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
|
== 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.
|
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
|
=== Restriction Option Shorthand
|
||||||
|
|
||||||
|
|||||||
@ -45,7 +45,7 @@ module ValidatesTimeliness
|
|||||||
begin
|
begin
|
||||||
restriction_value = type_cast(evaluate_option_value(options[restriction], record))
|
restriction_value = type_cast(evaluate_option_value(options[restriction], record))
|
||||||
unless value.send(RESTRICTIONS[restriction], restriction_value)
|
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
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
unless ValidatesTimeliness.ignore_restriction_errors
|
unless ValidatesTimeliness.ignore_restriction_errors
|
||||||
|
|||||||
@ -130,4 +130,13 @@ describe ValidatesTimeliness::Validator do
|
|||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user