diff --git a/lib/client_side_validations/active_model.rb b/lib/client_side_validations/active_model.rb index d927eb57..a492f6db 100644 --- a/lib/client_side_validations/active_model.rb +++ b/lib/client_side_validations/active_model.rb @@ -18,7 +18,24 @@ def copy_conditional_attributes(attribute_to, attribute_from) private def build_client_side_hash(model, attribute, options) - { message: model.errors.generate_message(attribute, message_type, options) }.merge(options.except(*callbacks_options - %i[allow_blank if unless])) + # Rails mutates `options` object when calling `model.errors.generate_message` + # by removing `message` option, if any. + # By raising on missing translations, CSV has the same behavior across + # all supported Rails versions and `config.i18n.raise_on_missing_translations` + # possible configurations. + options[:raise] = true + + message = + begin + model.errors.generate_message(attribute, message_type, options) + rescue I18n::MissingTranslationData + options[:message] = :invalid + model.errors.generate_message(attribute, message_type, options) + end + + options.delete(:raise) + + { message: message }.merge(options.except(*callbacks_options - %i[allow_blank if unless])) end def message_type diff --git a/test/active_model/cases/test_validations.rb b/test/active_model/cases/test_validations.rb index 0e26c1b1..8600d7e9 100644 --- a/test/active_model/cases/test_validations.rb +++ b/test/active_model/cases/test_validations.rb @@ -484,6 +484,23 @@ def test_multiple_validators_of_same_type_on_same_attribute assert_equal expected_hash, person.client_side_validation_hash end + def test_missing_translation + person = new_person do |p| + p.validates :first_name, comparison: { other_than: :last_name } + end + + expected_hash = { + first_name: { + comparison: [{ + message: 'is invalid', + other_than: :last_name + }] + } + } + + assert_equal expected_hash, person.client_side_validation_hash + end + def test_ignored_procs_validators person = new_person do |p| p.validates :first_name, format: proc { |o| o.matcher }