Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for interpolating {{attribute}} in validation messages

  • Loading branch information...
commit b4c86e3088c10fdf195bcefb42a229510cdd9e5b 1 parent 8c8625e
@alistairholt alistairholt authored
View
9 lib/live_validations/adapter_base/validation_hook.rb
@@ -39,15 +39,16 @@ def run_validation(adapter_instance, callback)
end
# Returns either the :message specified, or the default I18n error message.
- def message_for(key,options={})
- handwritten_message || I18n.translate(key, {:scope => 'activerecord.errors.messages'}.merge(options))
+ def message_for(attribute, key,options={})
+ handwritten_message_for(attribute) || I18n.translate(key, {:scope => 'activerecord.errors.messages'}.merge(options))
end
- def handwritten_message
+ def handwritten_message_for(attribute)
return unless callback.options[:message]
I18n.backend.send(:interpolate, I18n.locale, callback.options[:message], {
- :model => adapter_instance.active_record_instance.class.human_name
+ :model => adapter_instance.active_record_instance.class.human_name,
+ :attribute => attribute
})
end
View
14 lib/live_validations/adapters/jquery_validations.rb
@@ -10,12 +10,12 @@ class JqueryValidations < LiveValidations::AdapterBase
validates :presence do |v, attribute|
v[:validators][attribute]['required'] = true
- v[:messages][attribute]['required'] = v.message_for(:blank)
+ v[:messages][attribute]['required'] = v.message_for(attribute, :blank)
end
validates :acceptance do |v, attribute|
v[:validators][attribute]['required'] = true
- v[:messages][attribute]['required'] = v.message_for(:accepted)
+ v[:messages][attribute]['required'] = v.message_for(attribute, :accepted)
end
validates :length do |v, attribute|
@@ -47,7 +47,7 @@ class JqueryValidations < LiveValidations::AdapterBase
when Range
v[:validators][attribute]['range'] = [enum.first, enum.last]
when Array
- add_custom_rule(v, attribute, Digest::SHA1.hexdigest(enum.inspect), "var list = #{enum.to_json}; for (var i=0; i<list.length; i++){if(list[i] == value) { return true; }}", v.message_for(:inclusion))
+ add_custom_rule(v, attribute, Digest::SHA1.hexdigest(enum.inspect), "var list = #{enum.to_json}; for (var i=0; i<list.length; i++){if(list[i] == value) { return true; }}", v.message_for(attribute, :inclusion))
end
end
@@ -57,7 +57,7 @@ class JqueryValidations < LiveValidations::AdapterBase
v[:validators][attribute]['digits'] = true
v[:validators][attribute]['required'] = true
- message = v.message_for(:not_a_number)
+ message = v.message_for(attribute, :not_a_number)
v[:messages][attribute]['digits'] = message
v[:messages][attribute]['required'] = message
end
@@ -66,20 +66,20 @@ class JqueryValidations < LiveValidations::AdapterBase
attribute_name = "#{attribute}_confirmation".to_sym
v[:validators][attribute_name]['equalTo'] = "##{v.prefix}_#{attribute}"
v[:validators][attribute_name]['required'] = true
- message = v.message_for(:confirmation)
+ message = v.message_for(attribute, :confirmation)
v[:messages][attribute_name]['equalTo'] = message
v[:messages][attribute_name]['required'] = message
end
validates :format do |v, attribute|
regex = v.regex.inspect
- add_custom_rule(v, attribute, Digest::SHA1.hexdigest(regex.inspect), "return #{regex}.test(value)", v.message_for(:invalid))
+ add_custom_rule(v, attribute, Digest::SHA1.hexdigest(regex.inspect), "return #{regex}.test(value)", v.message_for(attribute, :invalid))
end
validates :uniqueness do |v, attribute|
model_class = v.adapter_instance.active_record_instance.class.name
v[:validators][attribute]['remote'] = "/live_validations/uniqueness?model_class=#{model_class}"
- v[:messages][attribute]['remote'] = v.message_for(:taken)
+ v[:messages][attribute]['remote'] = v.message_for(attribute, :taken)
end
response :uniqueness do |r|
View
24 lib/live_validations/adapters/livevalidation_dot_com.rb
@@ -7,33 +7,33 @@ class LivevalidationDotCom < LiveValidations::AdapterBase
end
validates :presence do |v, attribute|
- v[:validators][attribute]['Presence'] = {:failureMessage => v.message_for(:blank)}
+ v[:validators][attribute]['Presence'] = {:failureMessage => v.message_for(attribute, :blank)}
end
validates :format do |v, attribute|
# FIXME: The regexp outputs as a string, not a regex, in the javascripts.
- v[:validators][attribute]['Format'] = {:pattern => v.regex, :failureMessage => v.message_for(:invalid)}
+ v[:validators][attribute]['Format'] = {:pattern => v.regex, :failureMessage => v.message_for(attribute, :invalid)}
end
validates :numericality do |v, attribute|
- v[:validators][attribute]["Numericality"] = {:onlyInteger => true, :failureMessage => v.message_for(:not_a_number)}
+ v[:validators][attribute]["Numericality"] = {:onlyInteger => true, :failureMessage => v.message_for(attribute, :not_a_number)}
end
validates :length do |v, attribute|
if v.callback.options[:minimum]
- v[:validators][attribute]["Length"] = {:minimum => v.callback.options[:minimum], :failureMessage => v.message_for(:too_short, :count => v.callback.options[:minimum])}
+ v[:validators][attribute]["Length"] = {:minimum => v.callback.options[:minimum], :failureMessage => v.message_for(attribute, :too_short, :count => v.callback.options[:minimum])}
end
if v.callback.options[:maximum]
- v[:validators][attribute]["Length"] = {:maximum => v.callback.options[:maximum], :failureMessage => v.message_for(:too_long, :count => v.callback.options[:maximum])}
+ v[:validators][attribute]["Length"] = {:maximum => v.callback.options[:maximum], :failureMessage => v.message_for(attribute, :too_long, :count => v.callback.options[:maximum])}
end
if v.callback.options[:within]
v[:validators][attribute]["Length"] = {
:minimum => v.callback.options[:within].first,
:maximum => v.callback.options[:within].last,
- :tooShortMessage => v.message_for(:too_short, :count => v.callback.options[:within].first),
- :tooLongMessage => v.message_for(:too_long, :count => v.callback.options[:within].last)
+ :tooShortMessage => v.message_for(attribute, :too_short, :count => v.callback.options[:within].first),
+ :tooLongMessage => v.message_for(attribute, :too_long, :count => v.callback.options[:within].last)
}
end
@@ -51,22 +51,22 @@ class LivevalidationDotCom < LiveValidations::AdapterBase
when Range
case enum.first
when Numeric
- v[:validators][attribute]["Numericality"] = {:minimum => enum.first, :maximum => enum.last, :failureMessage => v.message_for(:inclusion)}
+ v[:validators][attribute]["Numericality"] = {:minimum => enum.first, :maximum => enum.last, :failureMessage => v.message_for(attribute, :inclusion)}
else
- v[:validators][attribute]["Inclusion"] = {:within => enum.to_a, :failureMessage => v.message_for(:inclusion)}
+ v[:validators][attribute]["Inclusion"] = {:within => enum.to_a, :failureMessage => v.message_for(attribute, :inclusion)}
end
when Array
- v[:validators][attribute]["Inclusion"] = {:within => enum.to_a, :failureMessage => v.message_for(:inclusion)}
+ v[:validators][attribute]["Inclusion"] = {:within => enum.to_a, :failureMessage => v.message_for(attribute, :inclusion)}
end
end
validates :exclusion do |v, attribute|
enum = v.callback.options[:in] || v.callback.options[:within]
- v[:validators][attribute]["Exclusion"] = {:within => enum.to_a, :failureMessage => v.message_for(:exclusion)}
+ v[:validators][attribute]["Exclusion"] = {:within => enum.to_a, :failureMessage => v.message_for(attribute, :exclusion)}
end
validates :acceptance do |v, attribute|
- v[:validators][attribute]["Acceptance"] = {:failureMessage => v.message_for(:accepted)}
+ v[:validators][attribute]["Acceptance"] = {:failureMessage => v.message_for(attribute, :accepted)}
end
validates :confirmation do |v, attribute|
View
8 test/general_adapter_test.rb
@@ -16,19 +16,19 @@ def test_message_for_with_custom_message
@hook.expects(:callback).at_least_once.returns(OpenStruct.new(:options => {:message => "Yep!"}))
@hook.expects(:adapter_instance).returns(OpenStruct.new(:active_record_instance => @post))
- assert_equal "Yep!", @hook.message_for(:blank)
+ assert_equal "Yep!", @hook.message_for(:title, :blank)
end
def test_message_for_with_l18n_fancy_message
- @hook.expects(:callback).at_least_once.returns(OpenStruct.new(:options => {:message => "The {{model}}"}))
+ @hook.expects(:callback).at_least_once.returns(OpenStruct.new(:options => {:message => "The {{model}} {{attribute}}"}))
@hook.expects(:adapter_instance).returns(OpenStruct.new(:active_record_instance => @post))
- assert_equal "The Post", @hook.message_for(:blank)
+ assert_equal "The Post title", @hook.message_for(:title, :blank)
end
def test_message_for_without_custom_message
@hook.expects(:callback).returns(OpenStruct.new(:options => {}))
- assert_equal I18n.translate('activerecord.errors.messages')[:blank], @hook.message_for(:blank)
+ assert_equal I18n.translate('activerecord.errors.messages')[:blank], @hook.message_for(:title, :blank)
end
def test_utilizes_inline_javascript_with_data_and_proc
Please sign in to comment.
Something went wrong with that request. Please try again.