Default disable_with for simple_form submit
Asked Answered
C

5

6

I want to change the default behavior of my submit button in simple_form so that I needn't explicitly specify :disable_with => true for all my forms. How can I make this particular change in the simple_form.rb?

Cliffhanger answered 5/7, 2012 at 8:50 Comment(0)
A
6

This is a little different in newer versions of Rails, as setting the property disable_with is deprecated. I wrote an article on this: http://www.railsonmaui.com/blog/2014/02/23/simple-form-and-disable-processing-by-default/

Here's the new code:

SimpleForm::FormBuilder.class_eval do
  def submit_with_override(field, options = {})
    data_disable_with = { disable_with: 'Processing...' }
    options[:data] = data_disable_with.merge(options[:data] || {})
    submit_without_override(field, options)
  end
  alias_method_chain :submit, :override
end

And thanks to @Appster for the idea!

Astaire answered 24/2, 2014 at 8:58 Comment(0)
C
3

Adding this override to my simple_form.rb worked like a charm!

SimpleForm::FormBuilder.class_eval do
  def submit_with_override(field, options = {})
    submit_without_override(field, {:disable_with => 'saving...'}.merge(options))
  end
  alias_method_chain :submit, :override
end
Cliffhanger answered 23/7, 2012 at 10:31 Comment(1)
For me, this works with some buttons and not others. Specifically, it works with <%= f.button :submit %> but not, for example, <%= f.button :button %> Could you explain what this is code is doing so I can work out how I might tweak my own settings?Ecospecies
F
2

According to ActionView::Helpers::FormBuilder.submit, f.button accespts 1~2 parameters, so both of following codes should be worked.

  • f.submit "MyText", class: "my-btn"
  • f.submit class: "my-btn"

In my case, adding this codes to initialize file worked fine.

SimpleForm::FormBuilder.class_eval do
  def submit_with_override(value=nil, options={})
    value, options = nil, value if value.is_a?(Hash)
    data_disable_with = { disable_with: 'Processing...' }
    options[:data] = data_disable_with.merge(options[:data] || {})
    submit_without_override(value, options)
  end
  alias_method_chain :submit, :override
end

Hope it helps.

Fawne answered 3/9, 2014 at 10:43 Comment(0)
A
1

It didn't override any existing data- attributes on the submit button which is compatible with Rails 5.

module DisableDoubleClickOnSimpleForms
  def submit(field, options = {})
    if field.is_a?(Hash)
      field[:data] ||= {}
      field[:data][:disable_with] ||= field[:value] || 'Processing...'
    else
      options[:data] ||= {}
      options[:data][:disable_with] ||= options[:value] || 'Processing...'
    end
    super(field, options)
  end
end

SimpleForm::FormBuilder.prepend(DisableDoubleClickOnSimpleForms)
Alnico answered 25/4, 2016 at 6:27 Comment(0)
O
0

If you are trying to change it for a single item and not everything I thought I would share this.

I am using Rails v6.1.5 and Simple form and I ran into this Simple Form issue today.

I was trying to change the value of the submit button once pressed from

'Change LOGO'

to

'Updating LOGO'

I tried the normal way:

= f.button :submit, class: 'btn btn-sm btn-primary', value: 'Change LOGO', id: 'submitLogo',
                  onclick: "logoSubmitButton(event)", disable_with: 'Updating LOGO'

It gave no error but it kept changing the button to 'Updating Company' which is the name of the instance variable and the Model @company and Company respectively.

This is what I had to use to make it work. I didn't have to change anything on the simple_form.rb initializer file.

= f.button :submit, class: 'btn btn-sm btn-primary', value: 'Change LOGO', id: 'submitLogo',
                  onclick: "logoSubmitButton(event)", data: {disable_with: 'Updating LOGO'}

This is the golden nugget that made it work:

data: {disable_with: 'Updating LOGO'}

I hope this helps someone else...

Scott

Occasional answered 15/6, 2022 at 22:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.