Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

globalize3 and easy_globalize3_accessors validation

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

like image 640
nilid Avatar asked Mar 09 '26 02:03

nilid


1 Answers

validate :titles_validation

def titles_validation
  errors.add(:base, "your message") if [title_ru, title_en].all? { |value| value.blank? }
end
like image 184
apneadiving Avatar answered Mar 11 '26 15:03

apneadiving



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!