using validate_presence_of and validate_uniqueness_of with i18n raise error #53

Closed
anujaware opened this Issue Feb 17, 2012 · 14 comments

Projects

None yet
@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
Deradon commented May 7, 2012

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

Will provide code and/or failing test_case later on.

@danhart
danhart commented May 11, 2012

+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
moger777 commented Jun 1, 2012

+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
ozgor commented Jul 11, 2012

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
kanfet commented Jul 20, 2012

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
danhart commented Jul 20, 2012

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

@kanfet
kanfet commented Jul 20, 2012

@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
Member
gabebw commented Dec 27, 2012

#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
gerad commented Feb 8, 2013

+1

@andyw8
andyw8 commented May 2, 2013

#206 fixed this for me.

@mxie
Member
mxie commented May 3, 2013

@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 May 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment