heartcombo / simple_form

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
http://blog.plataformatec.com.br/tag/simple_form
MIT License
8.21k stars 1.31k forks source link

Adding a data-x attribute to the root <form>? #1841

Closed dmix closed 4 months ago

dmix commented 4 months ago

I'm trying to add a Stimulus handler to all forms:

I looked at the code in simple_form and the only option I see is to add a default_form_class:

https://github.com/heartcombo/simple_form/blob/main/lib/simple_form/action_view_extensions/form_helper.rb

Is there a way to append a data attribute to every form without having to pass a custom data attribute to each one?

I tried appending it to wrapper_html on the default wrapper but it didnt work

carlosantoniodasilva commented 4 months ago

Might I suggest overriding the simple_form_for helper and delegating to super instead of looking for a configuration? e.g. something like this on your app helper:

module ApplicationHelper
  def simple_form_for(record, options = {}, &block)
    options[:html] ||= {}
    options[:html][:"data-x"] ||= "zomg-it-works"
    super
  end
end

CleanShot 2024-05-09 at 15 34 58@2x

Alternatively, a custom form builder could work as well, but that might require changing simple_form_for calls across your views so maybe not something to start with.