simple_form.rb 8.0 KB

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