Skip to content

Commit

Permalink
Display the error text as the abide error
Browse files Browse the repository at this point in the history
  • Loading branch information
ahukkanen committed May 14, 2024
1 parent 26d0fe8 commit 099521b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/cells/decidim/plans/plan_form/contents_edit.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% object.contents.each do |content| %>
<%= fields_for "contents[#{content.section.id}]", content, namespace: "plans_section_#{content.section.id}" do |content_form| %>
<%= fields_for "contents[#{content.section.id}]", content, namespace: "plans_section_#{content.section.id}", builder: Decidim::Plans::FormBuilder do |content_form| %>
<% content_form.instance_variable_set(:@template, form.instance_variable_get(:@template)) %>
<% content = content_form.object %>
Expand Down
3 changes: 2 additions & 1 deletion app/cells/decidim/plans/section_edit_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def field_options(name = :body)
id: field_id(name),
label: false,
disabled: field_disabled?,
help_text: tooltip_help? ? nil : model.help
help_text: tooltip_help? ? nil : model.help,
abide_error: translated_attribute(model.section.error_text).presence
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def categories_select(form, name, collection, options = {}, html_options = {})
selected = form.object.send(name)
selected = selected.first if selected.is_a?(Array) && selected.length > 1
categories = categories_for_select(collection)
options[:abide_error] ||= translated_attribute(section.error_text).presence

form.select(name, options_for_select(categories, selected), options, html_options)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def show
# Returns nothing.
def scopes_picker_field(form, name, options: {}, html_options: {})
options[:label] ||= translated_attribute(section.body) if options[:label].nil?
options[:abide_error] ||= translated_attribute(section.error_text).presence
form.select(name, scopes_options(scopes_root), options, html_options)
end

Expand Down
1 change: 1 addition & 0 deletions lib/decidim/plans.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Plans
autoload :SectionTypeRegistry, "decidim/plans/section_type_registry"
autoload :LayoutManifest, "decidim/plans/layout_manifest"
autoload :LayoutRegistry, "decidim/plans/layout_registry"
autoload :FormBuilder, "decidim/plans/form_builder"

include ActiveSupport::Configurable

Expand Down
40 changes: 40 additions & 0 deletions lib/decidim/plans/form_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module Decidim
module Plans
class FormBuilder < Decidim::FormBuilder
private

# Customized to modify the options passed to abide.
def field_with_validations(attribute, options, html_options)
class_options = html_options || options

if error?(attribute)
class_options[:class] = class_options[:class].to_s
class_options[:class] += " is-invalid-input"
end

help_text = options.delete(:help_text)
abide_error = options.delete(:abide_error)
prefix = options.delete(:prefix)
postfix = options.delete(:postfix)

class_options = extract_validations(attribute, options).merge(class_options)

content = yield(class_options)
content += abide_error_element(attribute, abide_error) if class_options[:pattern] || class_options[:required]
content = content.html_safe

html = wrap_prefix_and_postfix(content, prefix, postfix)
html + error_and_help_text(attribute, options.merge(help_text: help_text))
end

def abide_error_element(attribute, abide_error = nil)
return super(attribute) if abide_error.blank?

# Override the abide text
content_tag(:span, abide_error, class: "form-error")
end
end
end
end

0 comments on commit 099521b

Please sign in to comment.