Skip to content

Commit

Permalink
Fix issue with field validators when code reloading is disabled
Browse files Browse the repository at this point in the history
When the form classes are cached, the current strategy for field
validation with non-multilingual answers did not work correctly.
  • Loading branch information
ahukkanen committed Sep 26, 2023
1 parent 56d9580 commit 56d5171
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/decidim/plans/optionally_translatable_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def optionally_translatable_attribute(name, type, *options)

def optionally_translatable_validate_presence(attribute, options = {})
if_conditional_proc = conditional_proc(options[:if])
unless_conditional_proc = conditional_proc(options[:unless])
multilingual_proc = proc { |record|
record.component.settings.multilingual_answers?
}
Expand All @@ -27,16 +26,20 @@ def optionally_translatable_validate_presence(attribute, options = {})
multilingual_options[:if] = proc { |record|
multilingual_proc.call(record) && if_conditional_proc.call(record)
}
localized_options[:unless] = proc { |record|
multilingual_proc.call(record) && unless_conditional_proc.call(record)
}

multilingual_options[:translatable_presence] = true
localized_options[:presence] = true

localized_attribute = "#{attribute}_#{current_locale}".to_sym
validates attribute, multilingual_options
validates localized_attribute, localized_options

I18n.available_locales.each do |locale|
opts = localized_options.dup
opts[:if] = proc { |record|
current_locale.to_sym == locale.to_sym && !multilingual_proc.call(record) && if_conditional_proc.call(record)
}
localized_attribute = "#{attribute}_#{locale}".to_sym
validates localized_attribute, opts
end
end

def translatable_attributes
Expand Down

0 comments on commit 56d5171

Please sign in to comment.