# RailsBootstrapForm [![Ruby](https://github.com/shivam091/rails_bootstrap_form/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/shivam091/rails_bootstrap_form/actions/workflows/main.yml) [![Gem Version](https://badge.fury.io/rb/rails_bootstrap_form.svg)](https://badge.fury.io/rb/rails_bootstrap_form) **rails_bootstrap_form** is a Rails form builder that makes it super easy to integrate [Bootstrap 5](https://getbootstrap.com/) forms into your Rails application. `rails_bootstrap_form`'s form helpers generate the form field and its label along with all the Bootstrap mark-up required for proper Bootstrap display. ## Minimum Requirements * Ruby 3.2.2+ (https://www.ruby-lang.org/en/downloads/branches/) * Rails 7.0+ (https://guides.rubyonrails.org/maintenance_policy.html) * Bootstrap 5.0+ (https://getbootstrap.com/docs/versions/) ## Installation Install Bootstrap 5. There are many ways to do this, depending on the asset pipeline you're using in your Rails application. One way is to use the gem that works with Sprockets. To do so, in a brand new Rails 7.0 application created _without_ the `--webpacker` option, add the `bootstrap` gem to your `Gemfile`: ```ruby gem "bootstrap", "~> 5.0" ``` And follow the remaining instructions in the [official bootstrap installation guide](https://github.com/twbs/bootstrap-rubygem#a-ruby-on-rails) for setting up `application.scss` and `application.js`. Add the `rails_bootstrap_form` gem to your `Gemfile`: ```ruby gem "rails_bootstrap_form", "~> 0.9.5" ``` Then: `bundle install` Depending on which CSS pre-processor you are using, adding the bootstrap form styles differs slightly. If you use Rails in the default mode without any pre-processor, you'll have to add the following line to your `application.css` file: ```css *= require rails_bootstrap_form ``` If you followed the [official bootstrap installation guide](https://github.com/twbs/bootstrap-rubygem#a-ruby-on-rails), you'll probably have switched to SCSS. In this case add the following line to your `application.scss`: ```scss @import "rails_bootstrap_form"; ``` ## Configuration `rails_bootstrap_form` can be used without any configuration. However, `rails_bootstrap_form` does have an optional configuration file at `config/initializers/rails_bootstrap_form.rb` for setting options that affect all generated forms in an application. This configuration file is created using the generator by running the command: ``` $ rails generate rails_bootstrap_form:install ``` Example: ```ruby # config/initializers/rails_bootstrap_form.rb RailsBootstrapForm.configure do |config| # to make forms non-compliant with W3C. config.default_form_attributes = {novalidate: true} end ``` The current configuration options are: | Option | Default value | Description | |---------------------------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `default_form_attributes` | | Set this option to `{novalidate: true}` to instruct `rails_bootstrap_form` to skip all HTML 5 validation. | ## Usage ### bootstrap_form_for To get started, use the `bootstrap_form_for` helper in place of the Rails [`form_for`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for) helper. Here's an example: ![bootstrap_form_for](https://github.com/shivam091/rails_bootstrap_form/assets/7858927/dfc5f03b-049a-4c12-b575-8298cf5551d7) ```erb <%= bootstrap_form_for(@user) do |f| %> <%= f.email_field :email %> <%= f.password_field :password %> <%= f.check_box :remember_me %> <%= f.primary "Log In" %> <% end %> ``` This generates the following HTML: ```html
You must first accept terms and conditions in order to continue
``` ### bootstrap_form_with To get started, use the `bootstrap_form_with` helper in place of the Rails [`form_with`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with) helper. Here's an example: ![bootstrap_form_with](https://github.com/shivam091/rails_bootstrap_form/assets/7858927/06c605b9-29e2-4670-9495-0e913508d73f) ```erb <%= bootstrap_form_with(model: @user) do |f| %> <%= f.email_field :email %> <%= f.password_field :password %> <%= f.check_box :remember_me %> <%= f.primary "Log In" %> <% end %> ``` This generates the following HTML: ```html
You must first accept terms and conditions in order to continue
``` ## Bootstrap Configuration Options Here's a list of all possible options you can pass via `bootstrap` option that can be attached to the `bootstrap_form_for`, `bootstrap_form_with`, `fields_for`, or any field helpers inside of it: | Option | Description | Default value | | ------ | ------------- | ----------- | | `disabled` | An option to disable **rails_bootstrap_form** helpers. Default rails form builder element is rendered when set to `true` | `false` | | `layout` | Controls layout of form and field helpers. It can be `vertical` `horizontal`, or `inline`. | `vertical` | | `field_class` | A CSS class that will be applied to all form fields. | `nil` | | `help_text` | Describes help text for the HTML field. Help text is automatically read from translation file. If you want to customize it, you can pass a string. You can also set it `false` if you do not want help text displayed. | `nil` | | `label_text` | An option to customize automatically generated label text. | `nil` | | `skip_label` | An option to control whether the label is to be displayed or not. | `false` | | `hide_label` | An option to control whether the label is only accessible to a screen readers. | `false` | | `hide_class` | A CSS class that will be used when the label is only accessible to a screen readers. | `visually-hidden` | | `label_class` | A CSS class that will be applied to all labels when layout is `vertical`. | `form-label` | | `additional_label_class` | An additional CSS class that will be added along with the existing label css classes. | `nil` | | `prepend` | Raw or HTML content to be prepended to the field. | `nil` | | `append` | Raw or HTML content to be appended to the field. | `nil` | | `additional_input_group_class` | An additional CSS class that will be added to existing input group wrapper css classes. | `nil` | | `floating` | An option to control whether the field should have a floating label. | `false` | | `static_field_class` | A CSS class that will be applied to all static fields. | `form-control-plaintext` | | `switch` | An option to control whether the check box should look like Bootstrap switches. | `false` | | `wrapper` | An option to control the HTML attributes and options that will be added to a field wrapper. You can set it false if you don't the field to be rendered in a wrapper. | `{}` | | `size` | An option to control the size of input groups, buttons, labels, and fields. It can be `sm` or `lg`. | `nil` | | `inline` | An option to group checkboxes and radio buttons on the same horizontal row. | `false` | | `label_col_class` | A CSS class that will be applied to all labels when layout is `horizontal`. | `col-form-label` | | `label_col_wrapper_class` | A CSS class for label column when layout is `horizontal`. | `col-sm-2` | | `field_col_wrapper_class` | A CSS class for field column when layout is `horizontal`. | `col-sm-10` | | `render_as_button` | An option to render submit button using ` ``` ## Static controls `rails_bootstrap_form` provides form helper `static_field` to render static controls which internally uses [text_field](#text_field) form helper. It sets `readonly` and `disabled` attributes on the text field. By default, `static_field` applies `form-control-plaintext` CSS class to the control but you can change it by using option `static_field_class`. You can create a static controls like this: ![static_field](https://github.com/shivam091/rails_bootstrap_form/assets/7858927/cb7cc64f-c405-4abb-8ca9-66fe4c2fff98) ```erb <%= form.static_field :email %> ``` This generates the following HTML: ```html
``` _`static_field` supports all the bootstrap options which are supported by `text_field`._ ## Floating Labels The `floating` option can be used to enable Bootstrap floating labels. This option is supported on text fields, text areas and dropdowns. Here's an example: ![floating_labels](https://github.com/shivam091/rails_bootstrap_form/assets/7858927/3976629d-4717-47b0-ab91-9a16e6c9ed5f) ```erb <%= bootstrap_form_for @user, bootstrap: {floating: true} do |form| %> <%= form.text_field :name %> <%= form.email_field :username %> <%= form.password_field :password %> <%= form.fields_for :address, include_id: false do |address_form| %> <%= address_form.text_area :street %> <%= address_form.select :country_id, options_for_select(::Country.pluck(:name, :id), address_form.object.country_id), {include_blank: "Select Country"} %> <% end %> <%= form.primary "Register" %> <% end %> ``` This generates the following HTML: ```html
``` _`rails_bootstrap_form` automatically disables floating labels for unsupported helpers._ ## Validation and Errors By default, `rails_bootstrap_form` generations in-line errors which appear below the field. ### Inline Errors By default, fields that have validation errors will be outlined in red and the error will be displayed below the field. Here's an example: ![inline_errors](https://github.com/shivam091/rails_bootstrap_form/assets/7858927/a1cef10d-ba21-4d97-97e7-919691e2afe3) ```erb <%= bootstrap_form_for @user do |form| %> <%= form.email_field :email %> <%= form.password_field :password %> <%= form.primary "Register" %> <% end %> ``` This generates the following HTML: ```html
can't be blank
Please use official email address
can't be blank
``` Inline errors are also supported if the field is wrapped inside of input group and has floating label: ![floating_inline_errors](https://github.com/shivam091/rails_bootstrap_form/assets/7858927/89deb618-3f06-463b-91fb-60c50794387c) ```erb <%= form.text_field :expected_ctc, bootstrap: {floating: true, prepend: "$", append: "0.0"} %> ``` This generates the following HTML: ```html
$
0.0
can't be blank
``` The `has-validation` CSS class is added to an input group when the field has errors. The `is-invalid` CSS class is added to floating label container when field with floating label has errors. ## Required Fields A label that is associated with a mandatory field is automatically annotated with a `required` CSS class. `rails_bootstrap_form` provides styling for required fields. You're also free to add any appropriate CSS to style required fields as desired. The label `required` class is determined based on the definition of a presence validator with the associated model attribute. Presently this is one of: `ActiveRecord::Validations::PresenceValidator` or `ActiveModel::Validations::PresenceValidator`. In cases where this behaviour is undesirable, use the required option to force the class to be present or absent: ![required_field](https://github.com/shivam091/rails_bootstrap_form/assets/7858927/d99043f0-c382-4597-81bb-e96f27033e65) ``` <%= form.date_field :birth_date, required: false %> <%= form.url_field :blog_url, required: true %> ``` This generates the following HTML: ```html
``` ### Required `belongs_to` associations Adding a form control for a `belongs_to` associated field will automatically pick up the associated presence validator. ![belongs_to_presence](https://github.com/shivam091/rails_bootstrap_form/assets/7858927/feb279d8-a742-42c2-b13d-8e5a0f60dfa2) ```erb <%= form.collection_radio_buttons :fruit_id, ::Fruit.all, :id, :name, {checked: form.object.fruit_id} %> ``` This generates the following HTML: ```html
Select your favorite fruit
``` ## Internationalization `rails_bootstrap_form` follows standard rails conventions so it's i18n-ready. See more [here](http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models) ## Other Tips ### Empty But Visible Labels Some third party plug-ins require an empty but visible label on an input control. The `hide_label` option generates a label that won't appear on the screen, but it's considered invisible and therefore doesn't work with such a plug-in. An empty label (e.g. `""`) causes the underlying Rails helpers to generate a label based on the field's attribute's name. The solution is to use a zero-width character for the label, or some other "empty" HTML. For example: ```erb bootstrap: {label_text: "​".html_safe} ``` or ```erb bootstrap: {label_text: "".html_safe} ``` ## Contributing I welcome contributions. If you wish to contribute in `rails_bootstrap_form`, please review the [Contributing](/CONTRIBUTING.md) document first. ## License Copyright 2023 [Harshal V. LADHE](https://github.com/shivam091), Released under the MIT License