Skip to content
This repository was archived by the owner on Mar 3, 2024. It is now read-only.

Commit 482c3d6

Browse files
committed
Configure simple_form for Bootstrap
1 parent 48b32cf commit 482c3d6

File tree

4 files changed

+667
-0
lines changed

4 files changed

+667
-0
lines changed

config/initializers/simple_form.rb

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# frozen_string_literal: true
2+
#
3+
# Uncomment this and change the path if necessary to include your own
4+
# components.
5+
# See https://github.com/plataformatec/simple_form#custom-components to know
6+
# more about custom components.
7+
# Dir[Rails.root.join('lib/components/**/*.rb')].each { |f| require f }
8+
#
9+
# Use this setup block to configure all options available in SimpleForm.
10+
SimpleForm.setup do |config|
11+
# Wrappers are used by the form builder to generate a
12+
# complete input. You can remove any component from the
13+
# wrapper, change the order or even add your own to the
14+
# stack. The options given below are used to wrap the
15+
# whole input.
16+
config.wrappers :default, class: :input,
17+
hint_class: :field_with_hint, error_class: :field_with_errors, valid_class: :field_without_errors do |b|
18+
## Extensions enabled by default
19+
# Any of these extensions can be disabled for a
20+
# given input by passing: `f.input EXTENSION_NAME => false`.
21+
# You can make any of these extensions optional by
22+
# renaming `b.use` to `b.optional`.
23+
24+
# Determines whether to use HTML5 (:email, :url, ...)
25+
# and required attributes
26+
b.use :html5
27+
28+
# Calculates placeholders automatically from I18n
29+
# You can also pass a string as f.input placeholder: "Placeholder"
30+
b.use :placeholder
31+
32+
## Optional extensions
33+
# They are disabled unless you pass `f.input EXTENSION_NAME => true`
34+
# to the input. If so, they will retrieve the values from the model
35+
# if any exists. If you want to enable any of those
36+
# extensions by default, you can change `b.optional` to `b.use`.
37+
38+
# Calculates maxlength from length validations for string inputs
39+
# and/or database column lengths
40+
b.optional :maxlength
41+
42+
# Calculate minlength from length validations for string inputs
43+
b.optional :minlength
44+
45+
# Calculates pattern from format validations for string inputs
46+
b.optional :pattern
47+
48+
# Calculates min and max from length validations for numeric inputs
49+
b.optional :min_max
50+
51+
# Calculates readonly automatically from readonly attributes
52+
b.optional :readonly
53+
54+
## Inputs
55+
# b.use :input, class: 'input', error_class: 'is-invalid', valid_class: 'is-valid'
56+
b.use :label_input
57+
b.use :hint, wrap_with: { tag: :span, class: :hint }
58+
b.use :error, wrap_with: { tag: :span, class: :error }
59+
60+
## full_messages_for
61+
# If you want to display the full error message for the attribute, you can
62+
# use the component :full_error, like:
63+
#
64+
# b.use :full_error, wrap_with: { tag: :span, class: :error }
65+
end
66+
67+
# The default wrapper to be used by the FormBuilder.
68+
config.default_wrapper = :default
69+
70+
# Define the way to render check boxes / radio buttons with labels.
71+
# Defaults to :nested for bootstrap config.
72+
# inline: input + label
73+
# nested: label > input
74+
config.boolean_style = :nested
75+
76+
# Default class for buttons
77+
config.button_class = 'btn'
78+
79+
# Method used to tidy up errors. Specify any Rails Array method.
80+
# :first lists the first message for each field.
81+
# Use :to_sentence to list all errors for each field.
82+
# config.error_method = :first
83+
84+
# Default tag used for error notification helper.
85+
config.error_notification_tag = :div
86+
87+
# CSS class to add for error notification helper.
88+
config.error_notification_class = 'error_notification'
89+
90+
# ID to add for error notification helper.
91+
# config.error_notification_id = nil
92+
93+
# Series of attempts to detect a default label method for collection.
94+
# config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
95+
96+
# Series of attempts to detect a default value method for collection.
97+
# config.collection_value_methods = [ :id, :to_s ]
98+
99+
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
100+
# config.collection_wrapper_tag = nil
101+
102+
# You can define the class to use on all collection wrappers. Defaulting to none.
103+
# config.collection_wrapper_class = nil
104+
105+
# You can wrap each item in a collection of radio/check boxes with a tag,
106+
# defaulting to :span.
107+
# config.item_wrapper_tag = :span
108+
109+
# You can define a class to use in all item wrappers. Defaulting to none.
110+
# config.item_wrapper_class = nil
111+
112+
# How the label text should be generated altogether with the required text.
113+
# config.label_text = lambda { |label, required, explicit_label| "#{required} #{label}" }
114+
115+
# You can define the class to use on all labels. Default is nil.
116+
# config.label_class = nil
117+
118+
# You can define the default class to be used on forms. Can be overriden
119+
# with `html: { :class }`. Defaulting to none.
120+
# config.default_form_class = nil
121+
122+
# You can define which elements should obtain additional classes
123+
# config.generate_additional_classes_for = [:wrapper, :label, :input]
124+
125+
# Whether attributes are required by default (or not). Default is true.
126+
# config.required_by_default = true
127+
128+
# Tell browsers whether to use the native HTML5 validations (novalidate form option).
129+
# These validations are enabled in SimpleForm's internal config but disabled by default
130+
# in this configuration, which is recommended due to some quirks from different browsers.
131+
# To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations,
132+
# change this configuration to true.
133+
config.browser_validations = false
134+
135+
# Collection of methods to detect if a file type was given.
136+
# config.file_methods = [ :mounted_as, :file?, :public_filename, :attached? ]
137+
138+
# Custom mappings for input types. This should be a hash containing a regexp
139+
# to match as key, and the input type that will be used when the field name
140+
# matches the regexp as value.
141+
# config.input_mappings = { /count/ => :integer }
142+
143+
# Custom wrappers for input types. This should be a hash containing an input
144+
# type as key and the wrapper that will be used for all inputs with specified type.
145+
# config.wrapper_mappings = { string: :prepend }
146+
147+
# Namespaces where SimpleForm should look for custom input classes that
148+
# override default inputs.
149+
# config.custom_inputs_namespaces << "CustomInputs"
150+
151+
# Default priority for time_zone inputs.
152+
# config.time_zone_priority = nil
153+
154+
# Default priority for country inputs.
155+
# config.country_priority = nil
156+
157+
# When false, do not use translations for labels.
158+
# config.translate_labels = true
159+
160+
# Automatically discover new inputs in Rails' autoload path.
161+
# config.inputs_discovery = true
162+
163+
# Cache SimpleForm inputs discovery
164+
# config.cache_discovery = !Rails.env.development?
165+
166+
# Default class for inputs
167+
# config.input_class = nil
168+
169+
# Define the default class of the input wrapper of the boolean input.
170+
config.boolean_label_class = 'checkbox'
171+
172+
# Defines if the default input wrapper class should be included in radio
173+
# collection wrappers.
174+
# config.include_default_input_wrapper_class = true
175+
176+
# Defines which i18n scope will be used in Simple Form.
177+
# config.i18n_scope = 'simple_form'
178+
179+
# Defines validation classes to the input_field. By default it's nil.
180+
# config.input_field_valid_class = 'is-valid'
181+
# config.input_field_error_class = 'is-invalid'
182+
end

0 commit comments

Comments
 (0)