Reputation: 372
I'm using gems: globalize3 and easy_globalize3_accessors. I have a problem with validations. For example, I have Post model:
class Post
translates :title, :content
globalize_accessors :locales => [:en, :ru], :attributes => [:title, :content]
validates :title, :content, :presence => true
end
and form:
= form_for @post do |f|
-I18n.available_locales.each do |locale|
= f.text_field "title_#{locale}"
= f.text_area "content_#{locale}"
it looks like in view (if I18n.locale = :ru):
<form action="/ru/posts" method="post">
<input id="post_title_ru" name="post[title_ru]" type="text" />
<textarea cols="40" id="post_content_ru" name="vision[content_ru]"></textarea>
<input id="post_title_en" name="post[title_en]" type="text" />
<textarea cols="40" id="post_content_en" name="vision[content_en]"></textarea>
<input name="commit" type="submit" value="Создать Видение" />
</form>
If I fill in the fields only in Russian, the validation passes, if I wanted to post was in English only, and fill only the English field (when I18n.locale = :ru), the validation fails
Title can't be blank
Content can't be blank
As I understand it, there is a problem in the attributes, validation checks only the first attributes :title_ru and :content_ru. And to the rest of attributes (:content_en and :title_en) check does not reach.
how to make a second data validator to check if the validation of the first group of attributes is not passed?
thanks in advance
Upvotes: 3
Views: 983
Reputation: 115541
validate :titles_validation
def titles_validation
errors.add(:base, "your message") if [title_ru, title_en].all? { |value| value.blank? }
end
Upvotes: 5
Reputation: 27374
The problem is that globalize3 is validating the title for whatever locale you are currently in. If you want to validate for every locale (and not just the current locale), you have to explicitly add validators for the attribute in each locale (as @apneadiving pointed out).
You should be able to generate these validators automatically by cycling through I18n.available_locales
:
class Post < ActiveRecord::Base
I18n.available_locales.each do |locale|
validates :"title_#{locale}", :presence => true
end
...
end
Upvotes: 3