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

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

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.

Parameters:

  • name (String)

    The name of the button (used in the name attribute)



30
31
32
33
34
# File 'lib/brut/front_end/forms/input_declarations.rb', line 30

def button(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.

Parameters:

  • name (String)

    The name of the input (used in the name attribute)

  • attributes (Hash) (defaults to: {})

    Attributes to be used on the tag that represent its contraints. See Brut::FrontEnd::Forms::InputDefinition



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.

Parameters:



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.

Parameters:

  • name (String)

    The name of the group (used in the name attribute)

  • attributes (Hash) (defaults to: {})

    Attributes to be used on the tag that represent its contraints. See RadioButtonGroupInputDefinition



57
58
59
60
61
# File 'lib/brut/front_end/forms/input_declarations.rb', line 57

def radio_button_group(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

Parameters:

  • name (String)

    The name of the input (used in the name attribute)

  • attributes (Hash) (defaults to: {})

    Attributes to be used on the tag that represent its contraints. See SelectInputDefinition



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