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:
<%= 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:
<%= 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?