Class: Brut::FrontEnd::Forms::SelectInput

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/brut/front_end/forms/select_input.rb

Overview

Like Input, this models a <SELECT>'s current state and validity.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_definition:, value:, index:) ⇒ SelectInput

Create the input with the given definition and value

Parameters:



16
17
18
19
20
21
# File 'lib/brut/front_end/forms/select_input.rb', line 16

def initialize(input_definition:, value:, index:)
  @input_definition = input_definition
  @validity_state = Brut::FrontEnd::Forms::ValidityState.new
  @index = index
  self.value=(value)
end

Instance Attribute Details

#typed_valueDate|Time|BigDecimal|String|true|false|nil (readonly)

The value of the input, as coerced to the type used to evaluate constraints. Returns nil if the value could not be coerced (which likely means there is or will be a constraint violation), or if it is a blank String.

Of note, type=color will return a String here, but that String should be parseable into a hex code, i.e. #XXXXXX, where each "X" is 0-9 or a-f. Further, type=time will also return a string in the format HH:MM or HH:MM:SS, where the time is in 24 hour time. Lastly, type=datetime-local will return a Time, even though the control allows the visitor to choose an invalid time. If there is no valueMissing constraint violation, but this attribute returns nil, #value will return the string sent by the browser, even if it's not a real timestamp.

Returns:

  • (Date|Time|BigDecimal|String|true|false|nil)

    the value of the input, as coerced to the type used to evaluate constraints. Returns nil if the value could not be coerced (which likely means there is or will be a constraint violation), or if it is a blank String.

    Of note, type=color will return a String here, but that String should be parseable into a hex code, i.e. #XXXXXX, where each "X" is 0-9 or a-f. Further, type=time will also return a string in the format HH:MM or HH:MM:SS, where the time is in 24 hour time. Lastly, type=datetime-local will return a Time, even though the control allows the visitor to choose an invalid time. If there is no valueMissing constraint violation, but this attribute returns nil, #value will return the string sent by the browser, even if it's not a real timestamp.



10
11
12
# File 'lib/brut/front_end/forms/select_input.rb', line 10

def typed_value
  @typed_value
end

#validity_stateBrut::FrontEnd::Forms::ValidityState (readonly)

Returns Validity state that captures the current constraint violations, if any.

Returns:



13
14
15
# File 'lib/brut/front_end/forms/select_input.rb', line 13

def validity_state
  @validity_state
end

#valueString

Returns the input's value. DO NOTE this returns a string. This is because HTML stores these values as Strings. When using a checkbox, in particular, the value will not be a boolean. You cannot do if input.value and expect that work. See #typed_value instead.

Returns:

  • (String)

    the input's value. DO NOTE this returns a string. This is because HTML stores these values as Strings. When using a checkbox, in particular, the value will not be a boolean. You cannot do if input.value and expect that work. See #typed_value instead.



7
8
9
# File 'lib/brut/front_end/forms/select_input.rb', line 7

def value
  @value
end

Instance Method Details

#server_side_constraint_violation(key, context = true) ⇒ Object

Set a server-side constraint violation on this input. This is essentially arbitrary, but note that key should not be a key used for client-side validations.

Parameters:

  • key (String|Symbol)

    the I18n key fragment that describes the server side constraint violation

  • context (Hash|nil) (defaults to: true)

    any interpolations required to render the message



46
47
48
# File 'lib/brut/front_end/forms/select_input.rb', line 46

def server_side_constraint_violation(key,context=true)
  @validity_state.server_side_constraint_violation(key: key, context: context)
end

#valid?true|false

Returns true if the underlying #validity_state has no constraint violations.

Returns:

  • (true|false)

    true if the underlying #validity_state has no constraint violations



51
# File 'lib/brut/front_end/forms/select_input.rb', line 51

def valid? = @validity_state.valid?