Class: Brut::FrontEnd::Forms::Input
- Inherits:
-
Object
- Object
- Brut::FrontEnd::Forms::Input
- 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
Instance Attribute Summary collapse
-
#typed_value ⇒ Date|Time|BigDecimal|String|true|false|nil
readonly
The value of the input, as coerced to the type used to evaluate constraints.
-
#validity_state ⇒ Brut::FrontEnd::Forms::ValidityState
readonly
Validity state that captures the current constraint violations, if any.
-
#value ⇒ String
The input's value.
Instance Method Summary collapse
-
#array? ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#initialize(input_definition:, value:, index:) ⇒ Input
constructor
Create the input with the given definition and value.
-
#max ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#maxlength ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#min ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#minlength ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#name ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#pattern ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#required ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#server_side_constraint_violation(key, context = true) ⇒ Object
Set a server-side constraint violation on this input.
-
#step ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#type ⇒ String|nil
The value from the
input_definition
given to the initializer. -
#valid? ⇒ true|false
True if the underlying #validity_state has no constraint violations.
Constructor Details
#initialize(input_definition:, value:, index:) ⇒ Input
Create the input with the given definition and value
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_value ⇒ Date|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.
50 51 52 |
# File 'lib/brut/front_end/forms/input.rb', line 50 def typed_value @typed_value end |
#validity_state ⇒ Brut::FrontEnd::Forms::ValidityState (readonly)
Returns Validity state that captures the current constraint violations, if any.
53 54 55 |
# File 'lib/brut/front_end/forms/input.rb', line 53 def validity_state @validity_state end |
#value ⇒ String
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.
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.
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? |
#max ⇒ String|nil
Returns the value from the input_definition
given to the
initializer.
|
# File 'lib/brut/front_end/forms/input.rb', line 65
|
#maxlength ⇒ String|nil
Returns the value from the input_definition
given to the
initializer.
|
# File 'lib/brut/front_end/forms/input.rb', line 69
|
#min ⇒ String|nil
Returns the value from the input_definition
given to the
initializer.
|
# File 'lib/brut/front_end/forms/input.rb', line 73
|
#minlength ⇒ String|nil
Returns the value from the input_definition
given to the
initializer.
|
# File 'lib/brut/front_end/forms/input.rb', line 77
|
#name ⇒ String|nil
Returns the value from the input_definition
given to the
initializer.
|
# File 'lib/brut/front_end/forms/input.rb', line 81
|
#pattern ⇒ String|nil
Returns the value from the input_definition
given to the
initializer.
|
# File 'lib/brut/front_end/forms/input.rb', line 85
|
#required ⇒ String|nil
Returns 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.
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 |
#step ⇒ String|nil
Returns the value from the input_definition
given to the
initializer.
|
# File 'lib/brut/front_end/forms/input.rb', line 93
|
#type ⇒ String|nil
Returns 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.
270 |
# File 'lib/brut/front_end/forms/input.rb', line 270 def valid? = @validity_state.valid? |