Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
Rails forms made easy.
Simple Form aims to be as flexible as possible while helping you with powerful components to create your forms. The basic goal of Simple Form is to not touch your way of defining the layout, letting you find the better design for your eyes. Most of the DSL was inherited from Formtastic, which we are thankful for and should make you feel right at home.
INFO: This README refers to Simple Form 5.0. For older releases, check the related branch for your version.
Add it to your Gemfile:
gem 'simple_form'Run the following command to install it:
bundle installRun the generator:
rails generate simple_form:installSimple Form can be easily integrated with Bootstrap 5.
Use the bootstrap option in the install generator, like this:
rails generate simple_form:install --bootstrapThis will add an initializer that configures Simple Form wrappers for Bootstrap 5's form controls. You have to be sure that you added a copy of the Bootstrap assets on your application.
For more information see the generator output, our example application code.
To generate wrappers that are compatible with Zurb Foundation 5, pass
the foundation option to the generator, like this:
rails generate simple_form:install --foundationPlease note that the Foundation wrapper does not support the :hint option by default. In order to
enable hints, please uncomment the appropriate line in config/initializers/simple_form_foundation.rb.
You will need to provide your own CSS styles for hints.
Please see the instructions on how to install Foundation in a Rails app.
If you want to use the country select, you will need the country_select gem, add it to your Gemfile:
gem 'country_select'If you don't want to use the gem you can easily override this behaviour by mapping the
country inputs to something else, with a line like this in your simple_form.rb initializer:
config.input_mappings = { /country/ => :string }Simple Form was designed to be customized as you need to. Basically it's a stack of components that are invoked to create a complete html input for you, which by default contains label, hints, errors and the input itself. It does not aim to create a lot of different logic from the default Rails form helpers, as they do a great job by themselves. Instead, Simple Form acts as a DSL and just maps your input type (retrieved from the column definition in the database) to a specific helper method.
To start using Simple Form you just have to use the helper it provides:
<%= simple_form_for @user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>This will generate an entire form with labels for user name and password as well, and render errors by default when you render the form with invalid data (after submitting for example).
You can overwrite the default label by passing it to the input method. You can also add a hint, an error, or even a placeholder. For boolean inputs, you can add an inline label as well:
<%= simple_form_for @user do |f| %>
<%= f.input :username, label: 'Your username please', error: 'Username is mandatory, please specify one' %>
<%= f.input :password, hint: 'No special characters.' %>
<%= f.input :email, placeholder: '[email protected]' %>
<%= f.input :remember_me, inline_label: 'Yes, remember me' %>
<%= f.button :submit %>
<% end %>In some cases you may want to disable labels, hints or errors. Or you may want to configure the html of any of them:
<%= simple_form_for @user do |f| %>
<%= f.input :username, label_html: { class: 'my_class' }, hint_html: { class: 'hint_class' } %>
<%= f.input :password, hint: false, error_html: { id: 'password_error' } %>
<%= f.input :password_confirmation, label: false %>
<%= f.button :submit %>
<% end %>It is also possible to pass any html attribute straight to the input, by using the :input_html
option, for instance:
<%= simple_form_for @user do |f| %>
<%= f.input :username, input_html: { class: 'special' } %>
<%= f.input :password, input_html: { maxlength: 20 } %>
<%= f.input :remember_me, input_html: { value: '1' } %>
<%= f.button :submit %>
<% end %>If you want to pass the same options to all inputs in the form (for example, a default class),
you can use the :defaults option in simple_form_for. Specific options in input call will
overwrite the defaults:
<%= simple_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f| %>
<%= f.input :username, input_html: { class: 'special' } %>
<%= f.input :password, input_html: { maxlength: 20 } %>
<%= f.input :remember_me, input_html: { value: '1' } %>
<%= f.button :submit %>
<% end %>Since Simple Form generates a wrapper div around your label and input by default, you can pass
any html attribute to that wrapper as well using the :wrapper_html option, like so:
<%= simple_form_for @user do |f| %>
<%= f.input :username, wrapper_html: { class: 'username' } %>
<%= f.input :password, wrapper_html: { id: 'password' } %>
<%= f.input :remember_me, wrapper_html: { class: 'options' } %>
<%= f.button :submit %>
<% end %>Required fields are marked with an * prepended to their labels.
By default all inputs are required. When the form object includes ActiveModel::Validations
(which, for example, happens with Active Record models), fields are required only when there is presence validation.
Otherwise, Simple Form will mark fields as optional. For performance reasons, this
detection is skipped on validations that make use of conditional options, such as :if and :unless.
And of course, the required property of any input can be overwritten as needed:
<%= simple_form_for @user do |f| %>
<%= f.input :name, required: false %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>By default, Simple Form will look at the column type in the database and use an
appropriate input for the column. For example, a column created with type
:text in the database will use a textarea input by default. See the section
Available input types and defaults for each column
type
for a complete list of defaults.
Simple Form also lets you overwrite the default input type it creates:
<%= simple_form_for @user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.input :description, as: :text %>
<%= f.input :accepts, as: :radio_buttons %>
<%= f.button :submit %>
<% end %>So instead of a checkbox for the accepts attribute, you'll have a pair of radio buttons with yes/no
labels and a textarea instead of a text field for the description. You can also render boolean
attributes using as: :select to show a dropdown.
It is also possible to give the :disabled option to Simple Form, and it'll automatically mark
the wrapper as disabled with a CSS class, so you can style labels, hints and other components inside
the wrapper as well:
<%= simple_form_for @user do |f| %>
<%= f.input :username, disabled: true, hint: 'You cannot change your username.' %>
<%= f.button :submit %>
<% end %>Simple Form inputs accept the same options as their corresponding input type helper in Rails:
<%= simple_form_for @user do |f| %>
<%= f.input :date_of_birth, as: :date, start_year: Date.today.year - 90,
end_year: Date.today.year - 12, discard_day: true,
order: [:month, :year] %>
<%= f.input :accepts, as: :boolean, checked_value: 'positive', unchecked_value: 'negative' %>
<%= f.button :submit %>
<% end %>By default, Simple Form generates a hidden field to handle the un-checked case for boolean fields.
Passing unchecked_value: false in the options for boolean fields will cause this hidden field to be omitted,
following the convention in Rails. You can also specify include_hidden: false to skip the hidden field:
<%= simple_form_for @user do |f| %>
<%= f.input :just_the_checked_case, as: :boolean, include_hidden: false %>
<%= f.button :submit %>
<% end %>Simple Form also allows you to use label, hint, input_field, error and full_error helpers (please take a look at the rdocs for each method for more info):
<%= simple_form_for @user do |f| %>
<%= f.label :username %>
<%= f.input_field :username %>
<%= f.hint 'No special characters, please!' %>
<%= f.error :username, id: 'user_name_error' %>
<%= f.full_error :token %>
<%= f.submit 'Save' %>
<% end %>Any extra option passed to these methods will be rendered as html option.
Error messages are HTML-escaped by default. If you need to render HTML in error messages
(e.g. links), mark them as html_safe. This works for both custom error messages and
model validation messages automatically added to inputs:
<%= f.error :name, error: 'please see <a href="/help">our guide</a>'.html_safe %>Simple Form also allows you to strip away all the div wrappers around the <input> field that is
generated with the usual f.input.
The easiest way to achieve this is to use f.input_field.
Example:
simple_form_for @user do |f|
f.input_field :name
f.input_field :remember_me, as: :boolean
end<form>
...
<input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text">
<input name="user[remember_me]" type="hidden" value="0">
<label class="checkbox">
<input class="boolean optional" id="user_published" name="user[remember_me]" type="checkbox" value="1">
</label>
</form>For check boxes and radio buttons you can remove the label changing boolean_style from default value :nested to :inline.
Example:
simple_form_for @user do |f|
f.input_field :name
f.input_field :remember_me, as: :boolean, boolean_style: :inline
end<form>
...
<input class="string required" id="user_name" maxlength="255" name="user[name]" size="25Label's for attribute doesn't match the id when nil is given in collection
`belongs_to` relationships do not show up as required on rails 7.1+
[Bug] Collection option for time zone input doesn't limit the selection
5.2.0 applies `vertical_select` wrapper despite `wrapper: :horizontal_form`
Add Support for `css-zero`
need to be able to create accessible input error labels
Proposal for customizable classes merging
Generate HTML5 date/time inputs by default
The state of enums in simple_form
Better support for hyphenated HTML attributes akin to `data` and `aria`