Jerome
Jerome

Reputation: 6189

i18n custom validation error handling

In a rails 3.2.16 app form submission, error messages are not flowing i18n-ized to the view. Based on the following model definition

  validate :not_overlap

  def not_overlap
    errors.add(:start, 'message') if overlaps?
  end

I have not been able to insert the translation code helper

The following is the locale file:

  activerecord:
    models: 
      optionrate: "Options"
    attributes:
      optionrate:
        start: "Start"
        end: "End"
    errors:
      models:
        optionrate:
          attributes:
            start:
              not_overlap: "Dates overlap existing options."
            end:
              not_overlap: "Dates overlap existing options"

I am also unsure about the

<% @optionrate.errors.full_messages.each do |msg| %><%= msg %>

command and its i18n...

...which sort of makes 3 rabbits to run after

  1. how to invoke a translation message
  2. how to codify it properly in the locale file
  3. how to also handle the generic msg for i18n I assume there is a hierarchy as to how these get processed. Combined it is a geometrically-growing mess of alternatives.

What am I doing wrong?

Upvotes: 0

Views: 242

Answers (1)

WoolyWonder
WoolyWonder

Reputation: 171

I think the second argument to errors.add needs to be the symbol of the error message you're trying to add. So:

errors.add(:start, :not_overlap)

You've probably fixed this by now though!

Upvotes: 1

Related Questions