Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using errors.add(:key, 'some text') doesn't translate :key in rails 5 #56

Closed
karlingen opened this issue Sep 17, 2017 · 1 comment · Fixed by #85
Closed

Using errors.add(:key, 'some text') doesn't translate :key in rails 5 #56

karlingen opened this issue Sep 17, 2017 · 1 comment · Fixed by #85

Comments

@karlingen
Copy link

karlingen commented Sep 17, 2017

Attribute names are no longer translated. I'm not sure where the problem is, wether it is with reform-rails, i18n and/or rails. Maybe I'm calling the error messages in a wrong way.

Example:

module Documents::Contract
  class Create < Reform::Form

    property :name
    validate :file_has_valid_format?

    private

    def file_has_valid_format?
      errors.add(:name, I18n.t('validation.messages.error.file_type_upload_invalid'))
    end
  end
end

In rails 4 :name was translated to whatever I had defined in activerecord.en.yml:

en:
  activerecord:
    attributes:
      document:
        name: "The X File"
        description: "Descriptionzz"

I'm getting the error message like this:

@form.errors.full_messages.join(". ")

Gemfile.lock:

trailblazer-rails (1.0.2)
      reform-rails (>= 0.1.4, < 0.2.0)
      trailblazer (>= 2.0.0, < 2.1.0)
      trailblazer-loader (>= 0.1.0)
@karlingen
Copy link
Author

Hmm.. Perhaps this PR fixes it: #35

oliverguenther added a commit to opf/openproject that referenced this issue Jan 8, 2018
The validator passed to AM::Error does not use the `i18n_scope` we
define on either Reform::Form or the model, but is delegated from the
included AM::Translations module.

This in turn causes I18n to lookup the scope `:activemodel`, failing the
localization.

trailblazer/reform-rails#56
trailblazer/reform-rails#35
oliverguenther added a commit to opf/openproject that referenced this issue Jan 9, 2018
The validator passed to AM::Error does not use the `i18n_scope` we
define on either Reform::Form or the model, but is delegated from the
included AM::Translations module.

This in turn causes I18n to lookup the scope `:activemodel`, failing the
localization.

trailblazer/reform-rails#56
trailblazer/reform-rails#35

[ci skip]
emaglio pushed a commit that referenced this issue Nov 16, 2020
* Override reform full_messages to use ActiveModel full_messages

Reform has its own full_messages implementation as we can see here
https://github.com/trailblazer/reform/blob/master/lib/reform/errors.rb#L29
and reform-rails ResultErrors class inherits from
Reform::Contract::Result::Errors, so when you call "full_messages" to
get the full error messages, it was calling reform full_messages
implementation instead of delegating to ActiveModel and this is a
problem for whom need to translate attribute names, because reform only
humanizes the attribute name.

Fixes: #56

* Fix full_messages to get full_messages of form collections

We iterate over each field and for fields which is value is a twin::collection we get the instance variable of @amv_errors and call full_messages which will return the full translated messages of the collection fields.

* Fix full_messages por nested property
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant