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_select
,:time_select
or:datetime_select
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).
For radio inputs that map to ActiveRecord enum
attributes, Formtastic will automatically
load in your enum options to be used as the radio button choices. This can be overridden with
the :collection
option, or augmented with I18n translations. See examples below.
The way on which Formtastic renders the value
attribute and label for each choice in the :collection
is
customisable (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_options, #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, #custom_choice_html_options, #default_choice_html_options, #legend_html, #value_as_class?
Methods included from Base::Collections
#collection, #collection_for_boolean, #collection_from_association, #collection_from_enum, #collection_from_enum?, #collection_from_options, #label_and_value_method, #label_and_value_method_from_collection, #label_method, #label_method_from_options, #raw_collection, #send_or_call, #send_or_call_or_object, #value_method, #value_method_from_options
Methods included from Base
#initialize, #removed_option!, #warn_and_correct_option!, #warn_deprecated_option!
Methods included from Base::Wrapping
#input_wrapping, #wrapper_classes, #wrapper_classes_raw, #wrapper_dom_id, #wrapper_html_options, #wrapper_html_options_raw
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 LocalizedString
Methods included from Base::Associations
#association, #association_primary_key, #belongs_to?, #has_many?, #reflection
Methods included from Base::Fileish
Methods included from Base::Validations
#autofocus?, #column_limit, #limit, #not_required_through_negated_validation!, #not_required_through_negated_validation?, #optional?, #readonly?, #readonly_attribute?, #readonly_from_options?, #required?, #required_attribute?, #responds_to_global_required?, #validation_integer_only?, #validation_limit, #validation_max, #validation_min, #validation_step, #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
148 149 150 151 152 153 154 |
# File 'lib/formtastic/inputs/radio_input.rb', line 148 def choice_html(choice) template.content_tag(:label, builder.(input_name, choice_value(choice), .merge((choice)).merge(:required => false)) << choice_label(choice), .merge(:for => choice_input_dom_id(choice), :class => nil) ) 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.
158 159 160 |
# File 'lib/formtastic/inputs/radio_input.rb', line 158 def super.merge(:for => nil) end |
#to_html ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/formtastic/inputs/radio_input.rb', line 133 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 |