Repository: heartcombo/simple_form Branch: main Commit: 55bec8024c3b Files: 122 Total size: 438.9 KB Directory structure: gitextract_7k1r7n03/ ├── .github/ │ ├── code-scanning.yml │ └── workflows/ │ └── test.yml ├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── Gemfile ├── ISSUE_TEMPLATE.md ├── MIT-LICENSE ├── README.md ├── Rakefile ├── bin/ │ └── test ├── gemfiles/ │ ├── Gemfile-rails-7-0 │ ├── Gemfile-rails-7-1 │ ├── Gemfile-rails-7-2 │ ├── Gemfile-rails-8-0 │ └── Gemfile-rails-main ├── lib/ │ ├── generators/ │ │ └── simple_form/ │ │ ├── USAGE │ │ ├── install_generator.rb │ │ └── templates/ │ │ ├── README │ │ ├── _form.html.erb │ │ ├── _form.html.haml │ │ ├── _form.html.slim │ │ └── config/ │ │ ├── initializers/ │ │ │ ├── simple_form.rb │ │ │ ├── simple_form_bootstrap.rb │ │ │ └── simple_form_foundation.rb │ │ └── locales/ │ │ └── simple_form.en.yml │ ├── simple_form/ │ │ ├── action_view_extensions/ │ │ │ ├── builder.rb │ │ │ └── form_helper.rb │ │ ├── components/ │ │ │ ├── errors.rb │ │ │ ├── hints.rb │ │ │ ├── html5.rb │ │ │ ├── label_input.rb │ │ │ ├── labels.rb │ │ │ ├── maxlength.rb │ │ │ ├── min_max.rb │ │ │ ├── minlength.rb │ │ │ ├── pattern.rb │ │ │ ├── placeholders.rb │ │ │ └── readonly.rb │ │ ├── components.rb │ │ ├── error_notification.rb │ │ ├── form_builder.rb │ │ ├── helpers/ │ │ │ ├── autofocus.rb │ │ │ ├── disabled.rb │ │ │ ├── readonly.rb │ │ │ ├── required.rb │ │ │ └── validators.rb │ │ ├── helpers.rb │ │ ├── inputs/ │ │ │ ├── base.rb │ │ │ ├── block_input.rb │ │ │ ├── boolean_input.rb │ │ │ ├── collection_check_boxes_input.rb │ │ │ ├── collection_input.rb │ │ │ ├── collection_radio_buttons_input.rb │ │ │ ├── collection_select_input.rb │ │ │ ├── color_input.rb │ │ │ ├── date_time_input.rb │ │ │ ├── file_input.rb │ │ │ ├── grouped_collection_select_input.rb │ │ │ ├── hidden_input.rb │ │ │ ├── numeric_input.rb │ │ │ ├── password_input.rb │ │ │ ├── priority_input.rb │ │ │ ├── range_input.rb │ │ │ ├── rich_text_area_input.rb │ │ │ ├── string_input.rb │ │ │ ├── text_input.rb │ │ │ └── weekday_input.rb │ │ ├── inputs.rb │ │ ├── map_type.rb │ │ ├── railtie.rb │ │ ├── tags.rb │ │ ├── version.rb │ │ ├── wrappers/ │ │ │ ├── builder.rb │ │ │ ├── leaf.rb │ │ │ ├── many.rb │ │ │ ├── root.rb │ │ │ └── single.rb │ │ └── wrappers.rb │ └── simple_form.rb ├── simple_form.gemspec └── test/ ├── action_view_extensions/ │ ├── builder_test.rb │ └── form_helper_test.rb ├── components/ │ ├── custom_components_test.rb │ └── label_test.rb ├── form_builder/ │ ├── association_test.rb │ ├── button_test.rb │ ├── error_notification_test.rb │ ├── error_test.rb │ ├── general_test.rb │ ├── hint_test.rb │ ├── input_field_test.rb │ ├── label_test.rb │ └── wrapper_test.rb ├── generators/ │ └── simple_form_generator_test.rb ├── inputs/ │ ├── boolean_input_test.rb │ ├── collection_check_boxes_input_test.rb │ ├── collection_radio_buttons_input_test.rb │ ├── collection_select_input_test.rb │ ├── color_input_test.rb │ ├── country_input_test.rb │ ├── datetime_input_test.rb │ ├── disabled_test.rb │ ├── discovery_test.rb │ ├── file_input_test.rb │ ├── general_test.rb │ ├── grouped_collection_select_input_test.rb │ ├── hidden_input_test.rb │ ├── numeric_input_test.rb │ ├── readonly_test.rb │ ├── required_test.rb │ ├── rich_text_area_input_test.rb │ ├── string_input_test.rb │ ├── text_input_test.rb │ ├── time_zone_input_test.rb │ └── weekday_input_test.rb ├── simple_form_test.rb ├── support/ │ ├── discovery_inputs.rb │ ├── misc_helpers.rb │ ├── mock_controller.rb │ └── models.rb └── test_helper.rb ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/code-scanning.yml ================================================ paths-ignore: - test/** ================================================ FILE: .github/workflows/test.yml ================================================ name: Test permissions: contents: read on: push: branches: - main pull_request: workflow_dispatch: jobs: test: strategy: fail-fast: false matrix: gemfile: - Gemfile - gemfiles/Gemfile-rails-main - gemfiles/Gemfile-rails-8-0 - gemfiles/Gemfile-rails-7-2 - gemfiles/Gemfile-rails-7-1 - gemfiles/Gemfile-rails-7-0 ruby: - '4.0' - '3.4' - '3.3' - '3.2' - '3.1' - '3.0' - '2.7' exclude: - gemfile: Gemfile ruby: '3.1' - gemfile: Gemfile ruby: '3.0' - gemfile: Gemfile ruby: '2.7' - gemfile: gemfiles/Gemfile-rails-main ruby: '3.2' - gemfile: gemfiles/Gemfile-rails-main ruby: '3.1' - gemfile: gemfiles/Gemfile-rails-main ruby: '3.0' - gemfile: gemfiles/Gemfile-rails-main ruby: '2.7' - gemfile: gemfiles/Gemfile-rails-8-0 ruby: '3.1' - gemfile: gemfiles/Gemfile-rails-8-0 ruby: '3.0' - gemfile: gemfiles/Gemfile-rails-8-0 ruby: '2.7' - gemfile: gemfiles/Gemfile-rails-7-2 ruby: '3.0' - gemfile: gemfiles/Gemfile-rails-7-2 ruby: '2.7' runs-on: ubuntu-latest env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps BUNDLE_GEMFILE: ${{ matrix.gemfile }} steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} bundler-cache: true # runs bundle install and caches installed gems automatically - run: bundle exec rake ================================================ FILE: .gitignore ================================================ .bundle/ pkg/ rdoc/ gemfiles/*.lock /.idea/ ================================================ FILE: CHANGELOG.md ================================================ ## 5.4.1 * Ruby 4.0 support (no changes required) * Support procs on validators for minlength/maxlength, and improve validators logic across the board to match Rails [#1859](https://github.com/heartcombo/simple_form/pull/1859) ## 5.4.0 * Add support for Ruby 3.4 and Rails 7.2/8.0/8.1. (no changes required) * Drop support for Rails < 7 and Ruby < 2.7. * Add `weekday` input. [#1846](https://github.com/heartcombo/simple_form/pull/1846) * Remove redundant `aria-required` attribute for required fields. [#1823](https://github.com/heartcombo/simple_form/pull/1823) * Integrate `:rich_text_area` with placeholders [#1842](https://github.com/heartcombo/simple_form/pull/1842) * Fix encrypted attributes improperly casted (later fixed in Rails) [#1836](https://github.com/heartcombo/simple_form/pull/1836) * Pass `base` object to `human_attribute_name` in labels [#1812](https://github.com/heartcombo/simple_form/pull/1812) ## 5.3.1 * Revert "Speed up input mapping lookup by avoiding rescuing exceptions" from v5.3.0, it caused a regression on dev/test environments with custom inputs. * Try a slightly different approach to input lookups, without relying on regexp, to see if that helps with performance as originally intended. * Add support to Ruby 3.3. (no changes required.) ## 5.3.0 * Add support for Rails 7.1. (no meaningful changes required.) * Add `SimpleForm.deprecator` to integrate with new application deprecators in Rails 7.1. * Remove test files from the gem package. [@orien](https://github.com/orien) * Speed up input mapping lookup by avoiding rescuing exceptions. [@meanphil](https://github.com/meanphil) [@kriom](https://github.com/kriom) [@egeek](https://github.com/egeek) ## 5.2.0 * Add support for Rails 7.0 and Ruby 3.1/3.2 (no changes required) * Fix escaping issue on boolean input with `include_hidden: false` and custom wrapper. * Update Bootstrap install generator version 5. [@mhw](https://github.com/mhw) * Accept proc as `group_method` for grouped collection select * Honor `include_hidden` option on inline boolean inputs [@yboulkaid](https://github.com/yboulkaid) * Fix deprecation error when using country_select input. ## 5.1.0 * Remove `I18nCache` module entirely. It was added complexity for very little gain in some translations, and caused extra trouble upgrading to Ruby 3. If you need that level of caching consider looking into I18n caching as a whole. * Add support for Ruby 3.0, drop support for Ruby < 2.5. * Add support for Rails 6.1, drop support for Rails < 5.2. * Move CI to GitHub Actions. ## 5.0.3 ### Bug fix * Fix for ActiveStorage::Attached::Many. [@enriquez](https://github.com/enriquez) ## 5.0.2 ### Enhancements * Remove instruction to use form-inline class. [@goalaleo](https://github.com/goalaleo) * Added RichTextAreaInput for ActionText. [itsterry](https://github.com/itsterry) * Skip valid_class check if no class defined. [TALlama](https://github.com/TALlama) ### Bug fix * Fix 'aria-required' field generated by prompt. [@CarlosAlbertoSantos](https://github.com/CarlosAlbertoSantos) ## 5.0.1 ### Bug fix * Replace `_url` with `remote_url` when trying to guess file inputs [@tegon](https://github.com/tegon). This has the side-effect of changing carrierwave's support from `0.2.1` to `0.2.2`. ## 5.0.0 ### Enhancements * Set multiple attribute for grouped selects also. [@ollym](https://github.com/ollym) * Removes or renames label classes. [Abduvakilov](https://github.com/Abduvakilov) * Support to label custom classes for inline collections. [@feliperenan](https://github.com/feliperenan) * Update bootstrap generator template to match v4.3.x. [@m5o](https://github.com/m5o) * Allow "required" attribute in generated select elements of PriorityInput. [@mcountis](https://github.com/mcountis) ### Bug fix * Do not call `#send` in form object to check whether the attribute is a file input. [@tegon](https://github.com/tegon) ## Deprecations * The config `SimpleForm.file_methods` is deprecated and it has no effect. Simple Form now supports automatically discover of file inputs for the following Gems: activestorage, carrierwave, paperclip, refile and shrine. If you are using a custom method that is not from one of the supported Gems, please change your forms to pass the input type explicitly: ```erb <%= form.input :avatar, as: :file %> ``` See http://blog.plataformatec.com.br/2019/09/incorrect-access-control-in-simple-form-cve-2019-16676 for more information. ## 4.1.0 ### Enhancements * Guess input type more carefully. [@sringling](https://github.com/sringling) * Allow custom error on forms without model. [@victorperez](https://github.com/victorperez) * Do not support Ruby < 2.3 anymore. [@gssbzn](https://github.com/gssbzn) * Add color input type. [@gssbzn](https://github.com/gssbzn) ### Bug fix * Improve disabled option to input_field. [@betelgeuse](https://github.com/betelgeuse) * Memoize `input_html_classes` in `SimpleForm::Inputs::Base`. [@RigoTheDev](https://github.com/RigoTheDev) * Fix column type citext HTML5 input type bug. [@brucew](https://github.com/brucew) * Use form attribute in the nested boolean hidden field when it is given. [@feliperenan](https://github.com/feliperenan) ## 4.0.1 ### Bug fix * Do not support Rails 4 anymore. [@rafaelfranca](https://github.com/rafaelfranca) * Add missing comma. [@vill](https://github.com/vill) ## 4.0.0 ### Enhancements * Add bootstrap v4.1 generator template. [@m5o](https://github.com/m5o) * Add Rails 5.2 support. [@gobijan](https://github.com/gobijan) * Add API to register custom components.[@feliperenan](https://github.com/feliperenan) * Allow custom errors classes to inputs.[@feliperenan](https://github.com/feliperenan) * Remove support from Rails 4.0, 4.1 and 4.2. [@feliperenan](https://github.com/feliperenan) * Add support for citext, hstore, json & jsonb column types. [@swrobel](https://github.com/swrobel) * Add :valid_class on input wrapper when value is present and valid [@aeberlin](https://github.com/aeberlin), [@m5o](https://github.com/m5o) * Allow :valid_class to inputs when value is present and valid. [@m5o](https://github.com/m5o) * Allow validation classes on input_field. [@feliperenan](https://github.com/feliperenan) * Add basic ActiveStorage support. [@murb](https://github.com/murb) ### Bug fix * Fix horizontal form label position, from right to text-right. [@cavpollo](https://github.com/cavpollo) * Add base error display alongside existing errors. [@bluefalcon26](https://github.com/bluefalcon26) * Silent deprecation warning for placeholder_text. [@moofkit](https://github.com/moofkit) * Use custom i18n scope for label required html. [@tvdeyen](https://github.com/tvdeyen) ## 3.5.1 ### Enhancements * Exclude hidden field when unchecked_value: false. [@fschwahn](https://github.com/fschwahn) * Add frozen_string_literal magic comment to several files. [@oniofchaos](https://github.com/oniofchaos) * Try convert @object to model in case we got decorated object [@timurvafin](https://github.com/timurvafin) - From now, if you are using some object that inherits from `SimpleDelegator`, you must implement `def to_model; self; end`. Otherwise, *Simple Form* will convert the decorated object to the model since `SimpleDelegator` will delegate it to the model. * Code cleanup [@Fornacula](https://github.com/Fornacula) ### Bug fix * Fix error when the scope from association has parameter. [@feliperenan](https://github.com/feliperenan) * Only call `where` on associations when they respond to it. [@anicholson](https://github.com/anicholson) * require 'action_pack' before using it. [@etagwerker](https://github.com/etagwerker) * Check if Rails.env is defined. [@etagwerker](https://github.com/etagwerker) * Fix minlength. [@mameier](https://github.com/mameier) * Make errors_on_attribute return [] when not present. [@redrick](https://github.com/redrick) * Fix boolean inputs in nested style for label non-string. [@feliperenan](https://github.com/feliperenan) ## 3.5.0 * Updated gem dependency to support Rails 5.1.x. ## 3.4.0 * Removed Ruby 2.4.0 `Integer` unification deprecation warning. * Removed EOL Ruby 1.9.3 from the build matrix. * Added `minlength` component. * `boolean_label_class` can be set on a per-input basis. ## 3.3.1 ### Bug fix * Fix support for symbols when looking up types with `ActiveModel::Type`. ## 3.3.0 ### enhancements * Add the `aria-invalid` attribute on inputs with errors. * Added support for the new `ActiveModel::Type` API over Active Record's column objects. ### bug fix * Fix `merge_wrapper_options` to correctly merge options with duplicated keys. [@herminiotorres](https://github.com/herminiotorres) Closes [#1278](https://github.com/heartcombo/simple_form/issues/1278). ## 3.2.1 ### enhancements * Updated gem dependency to support Rails 5.0.x. ## 3.2.0 ### bug fix * Improve performance of input generation by disabling support for `_html` translations. This reverts the feature introduced on the 3.1.0 branch ## 3.1.1 ### enhancements * Add the `disabled_class` to the label when the input is disabled. [@rhodrid](https://github.com/rhodrid) ### bug fix * Make it possible to override `required` value that was previously set in the wrapper. [@nashby](https://github.com/nashby) * `date/time/datetime` inputs now correctly generate the label `for` attribute when HTML5 compatibility is explicitly enabled. [@ericsullivan](https://github.com/ericsullivan) * The datetime, date, and time inputs now have a nice format by default on bootstrap. [@ulissesalmeida](https://github.com/ulissesalmeida) [@eltonchrls](https://github.com/eltonchrls) * Now it is possible to set custom input mappings for collections. Example: ```ruby # On configuration: config.input_mappings = { /gender$/ => :check_boxes } # On form: f.input :gender, collection: [:male, :female] ``` [strangeworks](https://github.com/strangeworks) ## 3.1.0 ### enhancements * Update foundation generator to version 5. [@jorge-d](https://github.com/jorge-d) * Add mapping to `uuid` columns. * Add custom namespaces for custom inputs feature. [@vala](https://github.com/vala) * Add `:unless_blank` option to the wrapper API. [@IanVaughan](https://github.com/IanVaughan) * Add support to html markup in the I18n options. [@laurocaetano](https://github.com/laurocaetano) * Add the `full_error` component. [@laurocaetano](https://github.com/laurocaetano) * Add support to `scope` to be used on associations. [@laurocaetano](https://github.com/laurocaetano) * Execute the association `condition` in the object context. [@laurocaetano](https://github.com/laurocaetano) * Check if the given association responds to `order` before calling it. [@laurocaetano](https://github.com/laurocaetano) * Add Bootstrap 3 initializer template. * For radio or checkbox collection always use `:item_wrapper_tag` to wrap the content and add `label` when using `boolean_style` with `:nested` [@kassio](https://github.com/kassio) and [@erichkist](https://github.com/erichkist) * `input_field` uses the same wrapper as input but only with attribute components. [@nashby](https://github.com/nashby) * Add wrapper mapping per form basis [@rcillo](https://github.com/rcillo) and [@bernardoamc](https://github.com/bernardoamc) * Add `for` attribute to `label` when collections are rendered as radio or checkbox [@erichkist](https://github.com/erichkist), [@ulissesalmeida](https://github.com/ulissesalmeida) and [@fabioyamate](https://github.com/fabioyamate) * Add `include_default_input_wrapper_class` config [@luizcosta](https://github.com/luizcosta) * Map `datetime`, `date` and `time` input types to their respective HTML5 input tags when the `:html5` is set to `true` [@volmer](https://github.com/volmer) * Add `boolean_label_class` config. * Add `:html` option to include additional attributes on custom wrappers [@remofritzsche](https://github.com/remofritzsche) and [@ulissesalmeida](https://github.com/ulissesalmeida) * Make possible to use the Wrappers API to define attributes for the components. See https://github.com/heartcombo/simple_form/pull/997 for more information. * Put a whitespace before the `inline_label` options of boolean input if it is present. * Add support to configure the `label_text` proc at the wrapper level. [@NOX73](https://github.com/NOX73) * `label_text` proc now receive three arguments (label, request, and if the label was explicit). [@timscott](https://github.com/timscott) * Add I18n support to `:include_blank` and `:prompt` when `:translate` is used as value. [@haines](https://github.com/heartcombo/simple_form/pull/616) * Add support to define custom error messages for the attributes. * Add support to change the I18n scope to be used in Simple Form. [@nielsbuus](https://github.com/nielsbuus) * The default form class can now be overridden with `html: { :class }`. [@rmm5t](https://github.com/rmm5t) ### bug fix * Fix `full_error` when the attribute is an association. [@mvdamme](https://github.com/jorge-d) * Fix support to `:namespace` and `:index` options for nested check boxes and radio buttons when the attribute is an association. * Collection input that uses automatic collection translation properly sets checked values. Closes [#971](https://github.com/heartcombo/simple_form/issues/971) [@nashby](https://github.com/nashby) * Collection input generates `required` attribute if it has `prompt` option. [@nashby](https://github.com/nashby) * Grouped collection uses the first non-empty object to detect label and value methods. ## deprecation * Methods on custom inputs now accept a required argument with the wrapper options. See https://github.com/heartcombo/simple_form/pull/997 for more information. * SimpleForm.form_class is deprecated in favor of SimpleForm.default_form_class. Future versions of Simple Form will not generate `simple_form` class for the form element. See https://github.com/heartcombo/simple_form/pull/1109 for more information. Please check [v3.0](https://github.com/heartcombo/simple_form/blob/v3.0/CHANGELOG.md) for previous changes. ================================================ FILE: CONTRIBUTING.md ================================================ ## Contributing 1. If you have any questions about Simple Form, search the [Wiki](https://github.com/heartcombo/simple_form/wiki) or [Stack Overflow](http://stackoverflow.com/questions/tagged/simple_form). Do not post questions here. 2. If you find a security bug, **DO NOT** submit an issue here. Please send an e-mail to [heartcombo@googlegroups.com](mailto:heartcombo@googlegroups.com) instead. 3. Do a small search on the issues tracker before submitting your issue to see if it was already reported or fixed. In case it was not, create your report including Rails and Simple Form versions. If you are getting exceptions, please include the full backtrace. That's it! The more information you give, the more easy it becomes for us to track it down and fix it. Ideal scenario would be adding the issue to Simple Form test suite or to a sample application. Thanks! ================================================ FILE: Gemfile ================================================ source "https://rubygems.org" gemspec gem "activemodel", "~> 8.1.0" gem "actionpack", "~> 8.1.0" gem "railties", "~> 8.1.0" ================================================ FILE: ISSUE_TEMPLATE.md ================================================ ## Precheck - Do not use the issues tracker for help or support, try Stack Overflow. - For bugs, do a quick search and make sure the bug has not yet been reported - If you found a security bug, do not report it through GitHub. Please send an e-mail to heartcombo@googlegroups.com instead. - Finally, be nice and have fun! ## Environment - Ruby **[version]** - Rails **[version]** - Simple Form **[version]** ## Current behavior Include code samples, errors, steps to reproduce the error and stacktraces if appropriate. Will be even more helpful if you provide a sample application or a test case that reproduces the error. ## Expected behavior ================================================ FILE: MIT-LICENSE ================================================ Copyright (c) 2020-CURRENT Rafael França, Carlos Antonio da Silva Copyright (c) 2009-2019 Plataformatec Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ ![Simple Form Logo](https://raw.github.com/heartcombo/simple_form/main/simple_form.png) [![Gem Version](https://badge.fury.io/rb/simple_form.svg)](https://badge.fury.io/rb/simple_form) 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. ## Table of Contents - [Installation](#installation) - [Bootstrap](#bootstrap-5) - [Zurb Foundation 5](#zurb-foundation-5) - [Country Select](#country-select) - [Usage](#usage) - [Stripping away all wrapper divs](#stripping-away-all-wrapper-divs) - [Collections](#collections) - [Priority](#priority) - [Associations](#associations) - [Buttons](#buttons) - [Wrapping Rails Form Helpers](#wrapping-rails-form-helpers) - [Extra helpers](#extra-helpers) - [Simple Fields For](#simple-fields-for) - [Collection Radio Buttons](#collection-radio-buttons) - [Collection Check Boxes](#collection-check-boxes) - [Available input types and defaults for each column type](#available-input-types-and-defaults-for-each-column-type) - [Custom inputs](#custom-inputs) - [Custom form builder](#custom-form-builder) - [I18n](#i18n) - [Configuration](#configuration) - [The wrappers API](#the-wrappers-api) - [Custom Components](#custom-components) - [HTML 5 Notice](#html-5-notice) - [Using non Active Record objects](#using-non-active-record-objects) - [Information](#information) - [RDocs](#rdocs) - [Supported Ruby / Rails versions](#supported-ruby--rails-versions) - [Bug reports](#bug-reports) - [Maintainers](#maintainers) - [License](#license) ## Installation Add it to your Gemfile: ```ruby gem 'simple_form' ``` Run the following command to install it: ```console bundle install ``` Run the generator: ```console rails generate simple_form:install ``` ### Bootstrap 5 **Simple Form** can be easily integrated with [Bootstrap 5](https://getbootstrap.com/). Use the `bootstrap` option in the install generator, like this: ```console rails generate simple_form:install --bootstrap ``` This will add an initializer that configures **Simple Form** wrappers for Bootstrap 5's [form controls](https://getbootstrap.com/docs/5.0/forms/overview/). You have to be sure that you added a copy of the [Bootstrap](https://getbootstrap.com/) assets on your application. For more information see the generator output, our [example application code](https://github.com/heartcombo/simple_form-bootstrap). ### Zurb Foundation 5 To generate wrappers that are compatible with [Zurb Foundation 5](https://get.foundation/sites/docs-v5/), pass the `foundation` option to the generator, like this: ```console rails generate simple_form:install --foundation ``` Please 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](https://get.foundation/sites/docs-v5/applications.html). ### Country Select If you want to use the country select, you will need the [country_select gem](https://rubygems.org/gems/country_select), add it to your Gemfile: ```ruby 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: ```ruby config.input_mappings = { /country/ => :string } ``` ## Usage **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: ```erb <%= 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: ```erb <%= 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: 'user@domain.com' %> <%= 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: ```erb <%= 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: ```erb <%= 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: ```erb <%= 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: ```erb <%= 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: ```erb <%= 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](https://github.com/heartcombo/simple_form#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: ```erb <%= 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: ```erb <%= 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: ```erb <%= 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: ```erb <%= 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): ```erb <%= 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. ### Stripping away all wrapper divs **Simple Form** also allows you to strip away all the div wrappers around the `` field that is generated with the usual `f.input`. The easiest way to achieve this is to use `f.input_field`. Example: ```ruby simple_form_for @user do |f| f.input_field :name f.input_field :remember_me, as: :boolean end ``` ```html
...
``` For check boxes and radio buttons you can remove the label changing `boolean_style` from default value `:nested` to `:inline`. Example: ```ruby simple_form_for @user do |f| f.input_field :name f.input_field :remember_me, as: :boolean, boolean_style: :inline end ``` ```html
...
``` To view the actual RDocs for this, check them out here - https://rubydoc.info/github/heartcombo/simple_form/main/SimpleForm/FormBuilder:input_field ### Collections And what if you want to create a select containing the age from 18 to 60 in your form? You can do it overriding the `:collection` option: ```erb <%= simple_form_for @user do |f| %> <%= f.input :user %> <%= f.input :age, collection: 18..60 %> <%= f.button :submit %> <% end %> ``` Collections can be arrays or ranges, and when a `:collection` is given the `:select` input will be rendered by default, so we don't need to pass the `as: :select` option. Other types of collection are `:radio_buttons` and `:check_boxes`. Those are added by **Simple Form** to Rails set of form helpers (read Extra Helpers section below for more information). Collection inputs accept two other options beside collections: * *label_method* => the label method to be applied to the collection to retrieve the label (use this instead of the `text_method` option in `collection_select`) * *value_method* => the value method to be applied to the collection to retrieve the value Those methods are useful to manipulate the given collection. Both of these options also accept lambda/procs in case you want to calculate the value or label in a special way eg. custom translation. You can also define a `to_label` method on your model as **Simple Form** will search for and use `:to_label` as a `:label_method` first if it is found. By default, **Simple Form** will use the first item from an array as the label and the second one as the value. If you want to change this behavior you must make it explicit, like this: ```erb <%= simple_form_for @user do |f| %> <%= f.input :gender, as: :radio_buttons, collection: [['0', 'female'], ['1', 'male']], label_method: :second, value_method: :first %> <% end %> ``` All other options given are sent straight to the underlying Rails helper(s): [`collection_select`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select), [`collection_check_boxes`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_check_boxes), [`collection_radio_buttons`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_radio_buttons). For example, you can pass `prompt` and `selected` as: ```ruby f.input :age, collection: 18..60, prompt: "Select your age", selected: 21 ``` It may also be useful to explicitly pass a value to the optional `:selected` like above, especially if passing a collection of nested objects. It is also possible to create grouped collection selects, that will use the html *optgroup* tags, like this: ```ruby f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries ``` Grouped collection inputs accept the same `:label_method` and `:value_method` options, which will be used to retrieve label/value attributes for the `option` tags. Besides that, you can give: * *group_method* => the method to be called on the given collection to generate the options for each group (required) * *group_label_method* => the label method to be applied on the given collection to retrieve the label for the _optgroup_ (**Simple Form** will attempt to guess the best one the same way it does with `:label_method`) ### Priority **Simple Form** also supports `:time_zone` and `:country`. When using such helpers, you can give `:priority` as an option to select which time zones and/or countries should be given higher priority: ```ruby f.input :residence_country, priority: [ "Brazil" ] f.input :time_zone, priority: /US/ ``` Those values can also be configured with a default value to be used on the site through the `SimpleForm.country_priority` and `SimpleForm.time_zone_priority` helpers. Note: While using `country_select` if you want to restrict to only a subset of countries for a specific drop down then you may use the `:collection` option: ```ruby f.input :shipping_country, priority: [ "Brazil" ], collection: [ "Australia", "Brazil", "New Zealand"] ``` ### Associations To deal with associations, **Simple Form** can generate select inputs, a series of radios buttons or checkboxes. Lets see how it works: imagine you have a user model that belongs to a company and `has_and_belongs_to_many` roles. The structure would be something like: ```ruby class User < ActiveRecord::Base belongs_to :company has_and_belongs_to_many :roles end class Company < ActiveRecord::Base has_many :users end class Role < ActiveRecord::Base has_and_belongs_to_many :users end ``` Now we have the user form: ```erb <%= simple_form_for @user do |f| %> <%= f.input :name %> <%= f.association :company %> <%= f.association :roles %> <%= f.button :submit %> <% end %> ``` Simple enough, right? This is going to render a `:select` input for choosing the `:company`, and another `:select` input with `:multiple` option for the `:roles`. You can, of course, change it to use radio buttons and checkboxes as well: ```ruby f.association :company, as: :radio_buttons f.association :roles, as: :check_boxes ``` The association helper just invokes `input` under the hood, so all options available to `:select`, `:radio_buttons` and `:check_boxes` are also available to association. Additionally, you can specify the collection by hand, all together with the prompt: ```ruby f.association :company, collection: Company.active.order(:name), prompt: "Choose a Company" ``` In case you want to declare different labels and values: ```ruby f.association :company, label_method: :company_name, value_method: :id, include_blank: false ``` Please note that the association helper is currently only tested with Active Record. It currently does not work well with Mongoid and depending on the ORM you're using your mileage may vary. ### Buttons All web forms need buttons, right? **Simple Form** wraps them in the DSL, acting like a proxy: ```erb <%= simple_form_for @user do |f| %> <%= f.input :name %> <%= f.button :submit %> <% end %> ``` The above will simply call submit. You choose to use it or not, it's just a question of taste. The button method also accepts optional parameters, that are delegated to the underlying submit call: ```erb <%= f.button :submit, "Custom Button Text", class: "my-button" %> ``` To create a `