Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

using validate_presence_of and validate_uniqueness_of with i18n raise error #53

Closed
anujaware opened this Issue · 14 comments
@anujaware

It gives error
Failure/Error: it { should validate_uniqueness_of(:scheme_id).scoped_to(:designation_id) }
Expected errors to include "%{model} is already exist" when scheme_id is set to 1732, got errors: ["scheme_id Designations scheme is already exist (1732)", "scheme_id Sale Scheme Designations scheme is already exist (1732)", "scheme_type Designations scheme is already exist (3)"]

@halogenandtoast

Can you provide more example code to help us recreate this?

@Deradon

+1 on getting errors with "validate_presence_of" and i18n.

Will provide code and/or failing test_case later on.

@danhart

+1

Options are not interpolated into I18n by Shoulda::Matchers::ActiveModel::Helpers.default_error_messages:
https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/helpers.rb

In this file:
https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml
There is the information:
"# The values :model, :attribute and :value are always available for interpolation"

So, for example, I could place this in my application's en.yml file:

en:
  errors:
    messages:
      blank: "%{attribute} can't be blank"

And when I run the test I will get this:
Expected errors to include "%{attribute} can't be blank" when carousel_id is set to nil, got errors: carousel_id Carousel can't be blank (nil)title Title can't be blank (nil)

attribute has not been interpolated.

This might help resolve the issue: https://github.com/rails/rails/blob/master/activemodel/lib/active_model/errors.rb#L286

@moger777

+1

I get same error message when using ensure_length_of using interpolation with attribute. The only interpolation being passed in the hash is the count.

@ozgor

same here : Expected errors to include "doit être rempli(e)" when course is set to nil, got errors: ["course does not exist (nil)", "course_id does not exist (nil)"]

@kanfet

case_type.rb

class CaseType < ActiveRecord::Base
  validates :name, presence: true, uniqueness: true
end

case_type_spec.rb

require 'spec_helper'

describe CaseType do
  it { should validate_presence_of(:name) }
end

It works normally when default locale is english.
But when default locale is another (russian, for example), it raise error:

Failure/Error: it { should validate_presence_of(:name) }
       Expected errors to include "translation missing: ru.activerecord.errors.messages.blank" when name is set to nil, got errors: ["name translation missing: ru.activerecord.errors.models.case_type.attributes.name.blank (nil)"]
@danhart

@kanfet this is a separate issue to the one described in here. The issue described here is about i18n interpolation.

@kanfet

@danhart look at error text again, please. It about i18n interpolation. There is no locale files. But error is the same.

@kapiltekwani

+1

I am also facing the same issue

@gabebw
Owner

#206 might fix this, since it passes in :attribute.

@jesseclark

I have the same issue as @kanfet

I'm using matchers in Test/Unit and have some error messages overridden in locales/en but I am using the default message for uniqueness.

When I call should validate_uniqueness_of(:property_name)

I get:

Expected errors to include "translation missing: en.activerecord.errors.models.eligibility_question.attributes.property_name.taken" when property_name is set to "name", got errors: ["input_type is not a valid input type (nil)", "question_type is not a valid question type (nil)", "property_name is already taken (\"name\")"]

This seems to also be an i18n problem but not this interpolation issue. Should I open a new ticket?

@gerad

+1

@andyw8

#206 fixed this for me.

@mxie
Owner

@andyw8 - Thanks for checking!

With the changes from #206 and possibly other ones along the way, I believe this issue has been resolved. Closing.

@mxie mxie closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.