simple_form.rb 7.6 KB

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