Module: Brut::FrontEnd::Forms::InputDeclarations
- Included in:
- Brut::FrontEnd::Form
- Defined in:
- lib/brut/front_end/forms/input_declarations.rb
Overview
Extended by Brut::FrontEnd::Form to allow declaring inputs. This module creates methods per input on the form passed to your handlers. For example, if you have an ‘input :book_title`, then `form.book_title` will be available to access the value of the “book_title” input.
There are two methods that could be created, per input. Examples below use ‘book_title` as the attribute name
-
‘#book_title` - returns Brut::FrontEnd::Forms::Input#value, which is always a string.
-
‘#book_title_coerced` - returns Brut::FrontEnd::Forms::Input#typed_value, which is always the correct type for the input **or `nil` if type coercion failed**. Only call this once you have checked for constraint violations
For indexed parameters, the above methods require the index to be passed, e.g. ‘form.book_title_coerced(4)`. For non-indexed parameters, the index may not be passed.
Do not use this module directly. Instead, call #input or #select from within your form’s class definition.
Instance Method Summary collapse
-
#button(name) ⇒ Object
Declares a named button for this form, which is required in order to have this button’s name and value sent to the back.
-
#input(name, attributes = {}) ⇒ Object
Declares an input for this form, to be modeled via an HTML ‘<INPUT>` tag.
-
#inputs_from(other_class) ⇒ Object
Copy the inputs from another form into this one.
-
#radio_button_group(name, attributes = {}) ⇒ Object
Declares a radio button group, which will manifest as one or more ‘<input type=“radio”>` tags that all use the same value for their `name` attribute.
-
#select(name, attributes = {}) ⇒ Object
Declares a select for this form, to be modeled via an HTML ‘<SELECT>` tag.
Instance Method Details
#button(name) ⇒ Object
Declares a named button for this form, which is required in order to have this button’s name and value sent to the back. This will generate a ‘<button>` tag. To use `<input type=“submit”>`, #input should be used instead.
30 31 32 33 34 |
# File 'lib/brut/front_end/forms/input_declarations.rb', line 30 def (name) self.add_input_definition( Brut::FrontEnd::Forms::ButtonInputDefinition.new(name:) ) end |
#input(name, attributes = {}) ⇒ Object
Declares an input for this form, to be modeled via an HTML ‘<INPUT>` tag.
20 21 22 23 24 |
# File 'lib/brut/front_end/forms/input_declarations.rb', line 20 def input(name,attributes={}) self.add_input_definition( Brut::FrontEnd::Forms::InputDefinition.new(**(attributes.merge(name: name))) ) end |
#inputs_from(other_class) ⇒ Object
Copy the inputs from another form into this one. This is useful when one form should have identical inputs from another, plus a few of its own.
110 111 112 113 114 115 116 117 |
# File 'lib/brut/front_end/forms/input_declarations.rb', line 110 def inputs_from(other_class) if !other_class.respond_to?(:input_definitions) raise ArgumentError,"#{other_class} does not respond to #input_definitions - you cannot copy inputs from it" end other_class.input_definitions.each do |_name,input_definition| self.add_input_definition(input_definition) end end |
#radio_button_group(name, attributes = {}) ⇒ Object
Declares a radio button group, which will manifest as one or more ‘<input type=“radio”>` tags that all use the same value for their `name` attribute. Unlike `input` or `select`, this method is declaring one or more actual input tags.
Note that this is not where you would define the possible values for the group. That is done in Components::Inputs::RadioButton.
57 58 59 60 61 |
# File 'lib/brut/front_end/forms/input_declarations.rb', line 57 def (name,attributes={}) self.add_input_definition( Brut::FrontEnd::Forms::RadioButtonGroupInputDefinition.new(**(attributes.merge(name: name))) ) end |
#select(name, attributes = {}) ⇒ Object
Declares a select for this form, to be modeled via an HTML ‘<SELECT>` tag. Note that this will not define the values that appear in the select. That is done when the select is rendered, which you might do with a Components::Inputs::Select
42 43 44 45 46 |
# File 'lib/brut/front_end/forms/input_declarations.rb', line 42 def select(name,attributes={}) self.add_input_definition( Brut::FrontEnd::Forms::SelectInputDefinition.new(**(attributes.merge(name: name))) ) end |