Simple_form: disable the form without adding disabled: true or readonly: true for each input

I have a large simple_form form with fields that should be enabled or disabled depending on where the formal part is being loaded.

My question is: How do you quickly turn off each form input with simple helpers / wrappers simple_form?

A simple documentation form explains how disabled: trueyou can use it to disable a single input field :

<%= simple_form_for @user do |f| %>
  <%= f.input :username, disabled: true %>
  <%= f.button :submit %>
<% end %>

But the documentation is less clear how I can disable the entire form using simple_form helpers without having to repeat disabled: trueon literally every form input.

I tried to pass parameters disabled: trueand readonly: truein simple_form :wrapper_mappings, but it does not work.


Code example:

I load the form through partial definition of simple_form display variables. It works:

#user/show.html.erb:
<%= render partial: 'shared/form', locals: {questionnaire: @questionnaire, readonly_state: true, disabled_state: true, bootstrap_form_class: 'form-horizontal'} %>

However, readonly_state and disabled_state do not work unless I pass them to each form input:

# shared/_form.html.erb
<%= simple_form_for(@questionnaire, :html => {:class => bootstrap_form_class}, 

:wrapper_mappings => {check_boxes: :vertical_radio_and_checkboxes, file: :vertical_file_input,
boolean: :vertical_boolean  }) do |f| %>

  <%= f.input :username, disabled: disabled_state, hint: 'You cannot change your username.' %>
  <%= f.input :email, disabled: disabled_state %>
  <%= f.input :city, disabled: disabled_state %>
  <%= f.input :country, disabled: disabled_state %>
  . . .
  <%= f.button :submit %>
<% end %>

You can quickly see how this happens with a large shape.

How to disable and quickly format form attributes for an entire form using DRY code?

+4
3

, :

# config/initializers/simple_form.rb
config.wrappers :disabled_form do |b|
  b.use :input, disabled: true, readonly: true
end

:

<%= simple_form_for @model, wrapper: :disabled_form %>
  <%= f.input :field %>
  ...
<% end %>

wrapper_mapping .

+4

, jquery, $('.form input').prop('disabled', true);, form - .

+3

:

<%= f.input :username, disabled: true %>

'disabled' .

<%= f.input :username, input_html: {disabled: true} %>

:)

+3

All Articles