simple_form.rb 8.2 KB

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