Class: Brut::FrontEnd::Forms::InputDefinition

Inherits:
Object
  • Object
show all
Includes:
Brut::Framework::FussyTypeEnforcement
Defined in:
lib/brut/front_end/forms/input_definition.rb

Overview

Defines an input for a form, but not it's current runtime state. Input is used to understand the current state or value of an input.

Note that an input definition is defining an HTML <input>, not a generic attribute. Thus, the only constraints you can place on an input are those that the browser supports. If your form needs server side validation, you can accomplish that in a lot of ways, such as implementing a BackEnd::Validators::FormValidator, or calling Brut::FrontEnd::Form#server_side_constraint_violation directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Brut::Framework::FussyTypeEnforcement

#type!

Constructor Details

#initialize(max: nil, maxlength: nil, min: nil, minlength: nil, name: nil, pattern: :based_on_type, required: :based_on_type, step: nil, type: nil, array: false) ⇒ InputDefinition

Create an InputDefinition. This should very closely mirror the attributes used in an <INPUT> element in HTML. The idea is to be able to create HTML that validates its values the same as we can in Ruby so that client side validations can be safely used for user experience, but also re-executed server side.

Parameters:

  • min (Integer|Date|Time) (defaults to: nil)

    Minimum value allowed by the input. Not relevat to all types.

  • max (Integer|Date|Time) (defaults to: nil)

    Maximum value allowed by the input. Not relevat to all types.

  • minlength (Integer) (defaults to: nil)

    Minimum length of the value allowed.

  • maxlength (Integer) (defaults to: nil)

    Maximum length of the value allowed.

  • name (String) (defaults to: nil)

    Name of the input (required)

  • pattern (Regexp) (defaults to: :based_on_type)

    that the value must match. Note that this technically must be a regular expression that works for both Ruby and JavaScript, so don't get fancy.

  • required (true|false) (defaults to: :based_on_type)

    true if this field is required, false otherwise. Default is true unless type is "checkbox".

  • step (Integer) (defaults to: nil)

    Step value for ranged inputs. A value that is not on a step is considered invalid.

  • type (String) (defaults to: nil)

    the type of input to create. Should be a value from the HTML spec. Default is based on the value of name. If email, type is email. If password or password_confirmation, type is password. Otherwise text.

  • array (true|false) (defaults to: false)

    If true, the form will expect multiple values for this input. The values will be available as an array. Any values omitted by the user will be present as empty strings.

See Also:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/brut/front_end/forms/input_definition.rb', line 60

def initialize(
  max: nil,
  maxlength: nil,
  min: nil,
  minlength: nil,
  name: nil,
  pattern: :based_on_type,
  required: :based_on_type,
  step: nil,
  type: nil,
  array: false
)
  name = name.to_s
  type = if type.nil?
           case name
           when "email" then "email"
           when "password" then "password"
           when "password_confirmation" then "password"
           else
             "text"
           end
         else
           type
         end

  type = type.to_s
  if required == :based_on_type
    required = type != "checkbox"
  end

  @max       = max
  @maxlength = type!( maxlength , Numeric                   , "maxlength")
  @min       = min
  @minlength = type!( minlength , Numeric                   , "minlength")
  @name      = type!( name      , String                    , "name", required: true)
  @required  = type!( required  , [true, false]             , "required", required: true)
  @step      = type!( step      , Numeric                   , "step")
  @type      = type!( type      , INPUT_TYPES_TO_CLASS.keys , "type", required: true)
  @array     = type!( array     , [true, false]             , "array", required: true)

  @pattern = if pattern == :based_on_type 
               if type == "email"
                 /^[^@]+@[^@]+\.[^@]+$/.source
               else
                 nil
               end
             else
               type!( pattern, String, "pattern" )
             end
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



11
12
13
# File 'lib/brut/front_end/forms/input_definition.rb', line 11

def max
  @max
end

#maxlengthObject (readonly)

Returns the value of attribute maxlength.



12
13
14
# File 'lib/brut/front_end/forms/input_definition.rb', line 12

def maxlength
  @maxlength
end

#minObject (readonly)

Returns the value of attribute min.



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

def min
  @min
end

#minlengthObject (readonly)

Returns the value of attribute minlength.



14
15
16
# File 'lib/brut/front_end/forms/input_definition.rb', line 14

def minlength
  @minlength
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/brut/front_end/forms/input_definition.rb', line 15

def name
  @name
end

#patternObject (readonly)

Returns the value of attribute pattern.



16
17
18
# File 'lib/brut/front_end/forms/input_definition.rb', line 16

def pattern
  @pattern
end

#requiredObject (readonly)

Returns the value of attribute required.



17
18
19
# File 'lib/brut/front_end/forms/input_definition.rb', line 17

def required
  @required
end

#stepObject (readonly)

Returns the value of attribute step.



18
19
20
# File 'lib/brut/front_end/forms/input_definition.rb', line 18

def step
  @step
end

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/brut/front_end/forms/input_definition.rb', line 19

def type
  @type
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


111
# File 'lib/brut/front_end/forms/input_definition.rb', line 111

def array? = @array

#make_input(value:, index:) ⇒ Object

Create an Input based on this definition, initializing it with the given value. inputs. nil is allowed only if the input definition is not for an array.

Parameters:

  • value (String)

    the value to give this input initially.

  • index (Integer)

    the index of this input, if it is part of an array of



118
119
120
# File 'lib/brut/front_end/forms/input_definition.rb', line 118

def make_input(value:, index:)
  Brut::FrontEnd::Forms::Input.new(input_definition: self, value:, index:)
end