Class: Formtastic::Inputs::RadioInput
- Inherits:
-
Object
- Object
- Formtastic::Inputs::RadioInput
- Includes:
- Base, Base::Choices, Base::Collections
- Defined in:
- lib/formtastic/inputs/radio_input.rb
Overview
:disabled like CheckBoxes?
A radio input is used to render a series of radio inputs. This is an alternative input choice for ‘belongs_to` associations like a `Post` belonging to a `Section` or an `Author`, or any case where the user needs to make a single selection from a pre-defined collectioon of choices.
Within the standard ‘<li>` wrapper, the output is a `<fieldset>` with a `<legend>` to represent the “label” for the input, and an `<ol>` containing `<li>`s for each choice in the association. Each `<li>` choice has a `<label>` containing an `<input type=“radio”>` and the label text to describe each choice.
Radio inputs can be considered as an alternative where a (non-multi) select input is used, especially in cases where there are only a few choices, however they are not used by default for any type of association or model attribute. You can choose to use a radio input instead of a select with ‘:as => :radio`.
Like a select input, the flexibility of the ‘:collection` option (see examples) makes the :radio input viable as an alternative for many other input types. For example, instead of…
-
a ‘:string` input (where you want to force the user to choose from a few specific strings rather than entering anything)
-
a ‘:boolean` checkbox input (where the user could choose yes or no, rather than checking a box)
-
a ‘:date`, `:time` or `:datetime` input (where the user could choose from a small set of pre-determined dates)
-
a ‘:number` input (where the user could choose from a small set of pre-defined numbers)
-
a ‘:time_zone` input (where you want to provide your own small set of choices instead of relying on Rails)
-
a ‘:country` input (where you want to provide a small set of choices, no need for a plugin really)
For radio inputs that map to associations on the object model, Formtastic will automatically load in a collection of objects on the association as options to choose from. This might be an ‘Author.all` on a `Post` form with an input for a `belongs_to :user` association, or a `Section.all` for a `Post` form with an input for a `belongs_to :section` association. You can override or customise this collection through the `:collection` option (see examples).
The way on which Formtastic renders the ‘value` attribute and label for each choice is customisable through the `:label_method` and `:value_method` options (see examples below). When not provided, we fall back to a list of methods to try on each object such as `:to_label`, `:name` and `:to_s`, which are defined in the configurations `collection_label_methods` and `collection_value_methods`.
Instance Attribute Summary
Attributes included from Base
#builder, #method, #object, #object_name, #options, #template
Instance Method Summary collapse
- #choice_html(choice) ⇒ Object
-
#label_html_options ⇒ Object
Override to remove the for attribute since this isn’t associated with any element, as it’s nested inside the legend.
- #to_html ⇒ Object
Methods included from Base::Choices
#choice_html_safe_value, #choice_input_dom_id, #choice_label, #choice_value, #choice_wrapping, #choice_wrapping_html_options, #choices_group_wrapping, #choices_group_wrapping_html_options, #choices_wrapping, #choices_wrapping_html_options, #legend_html, #value_as_class?
Methods included from Base::Collections
#collection, #collection_for_boolean, #collection_from_association, #collection_from_options, #label_and_value_method, #label_method, #raw_collection, #send_or_call, #value_method
Methods included from Base
Methods included from Base::Wrapping
#input_wrapping, #wrapper_dom_id, #wrapper_html_options
Methods included from Base::Labelling
#label_from_options, #label_html, #label_text, #localized_label, #render_label?, #requirement_text, #requirement_text_or_proc
Methods included from Base::Associations
#association, #association_primary_key, #belongs_to?, #reflection
Methods included from Base::Fileish
Methods included from Base::Validations
#column_limit, #limit, #not_required_through_negated_validation!, #not_required_through_negated_validation?, #optional?, #required?, #validation_integer_only?, #validation_limit, #validation_max, #validation_min, #validations, #validations?, #validator_relevant?
Methods included from Base::Naming
#as, #attributized_method_name, #humanized_method_name, #input_name, #sanitized_method_name, #sanitized_object_name
Methods included from Base::Hints
#hint?, #hint_html, #hint_text, #hint_text_from_options
Methods included from Base::Errors
#error_first_html, #error_html, #error_keys, #error_list_html, #error_none_html, #error_sentence_html, #errors, #errors?
Methods included from Base::Database
Methods included from Base::Options
#formtastic_options, #input_options
Methods included from Base::Html
#dom_id, #dom_index, #input_html_options
Instance Method Details
#choice_html(choice) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/formtastic/inputs/radio_input.rb', line 130 def choice_html(choice) template.content_tag(:label, builder.(input_name, choice_value(choice), .merge(:id => choice_input_dom_id(choice))) << choice_label(choice), .merge(:for => choice_input_dom_id(choice)) ) end |
#label_html_options ⇒ Object
Override to remove the for attribute since this isn’t associated with any element, as it’s nested inside the legend.
140 141 142 |
# File 'lib/formtastic/inputs/radio_input.rb', line 140 def super.merge(:for => nil) end |
#to_html ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/formtastic/inputs/radio_input.rb', line 115 def to_html input_wrapping do choices_wrapping do legend_html << choices_group_wrapping do collection.map { |choice| choice_wrapping((choice)) do choice_html(choice) end }.join("\n").html_safe end end end end |