Class: Brut::FrontEnd::Forms::Input

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

Overview

An Input is a stateful object representing a specific input and its value during the course of a form submission process. In particular, it wraps a value and a ValidityState. These are mutable, whereas the wrapped InputDefinition is not.

Of note, this class also initiates the server-execution of the client-side validations. When a form is posted, and you ask the form to validate itself, this class examines the value for the inputs and determines if a client-side violation should've happened in the browser.

It does its best to mimic the browser's behavior, however there will be some subtle differences.

An important considering is that everything is a string. The browser wants strings and its internal API is string-based. So is this. That said, this will convert strings into richer types in order to perform constraint analysis.

Where possible, standard library classes are used, but in some cases - colors and times - there is no standard library class.

See #typed_value.

Defined Under Namespace

Classes: Color, TimeOfDay

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create the input with the given definition and value

Parameters:



58
59
60
61
62
63
# File 'lib/brut/front_end/forms/input.rb', line 58

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.



50
51
52
# File 'lib/brut/front_end/forms/input.rb', line 50

def typed_value
  @typed_value
end

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

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

Returns:



53
54
55
# File 'lib/brut/front_end/forms/input.rb', line 53

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.



33
34
35
# File 'lib/brut/front_end/forms/input.rb', line 33

def value
  @value
end

Instance Method Details

#array?String|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



105
106
107
108
109
110
111
112
113
114
# File 'lib/brut/front_end/forms/input.rb', line 105

def_delegators :"@input_definition", :max,
:maxlength,
:min,
:minlength,
:name,
:pattern,
:required,
:step,
:type,
:array?

#maxString|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



# File 'lib/brut/front_end/forms/input.rb', line 65

#maxlengthString|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



# File 'lib/brut/front_end/forms/input.rb', line 69

#minString|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



# File 'lib/brut/front_end/forms/input.rb', line 73

#minlengthString|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



# File 'lib/brut/front_end/forms/input.rb', line 77

#nameString|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



# File 'lib/brut/front_end/forms/input.rb', line 81

#patternString|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



# File 'lib/brut/front_end/forms/input.rb', line 85

#requiredString|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



# File 'lib/brut/front_end/forms/input.rb', line 89

#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



265
266
267
# File 'lib/brut/front_end/forms/input.rb', line 265

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

#stepString|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



# File 'lib/brut/front_end/forms/input.rb', line 93

#typeString|nil

Returns the value from the input_definition given to the initializer.

Returns:

  • (String|nil)

    the value from the input_definition given to the initializer



# File 'lib/brut/front_end/forms/input.rb', line 97

#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



270
# File 'lib/brut/front_end/forms/input.rb', line 270

def valid? = @validity_state.valid?