Class: Brut::FrontEnd::Forms::InputDefinition
- Inherits:
-
Object
- Object
- Brut::FrontEnd::Forms::InputDefinition
- 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
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#maxlength ⇒ Object
readonly
Returns the value of attribute maxlength.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#minlength ⇒ Object
readonly
Returns the value of attribute minlength.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#step ⇒ Object
readonly
Returns the value of attribute step.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#array? ⇒ Boolean
-
#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
constructor
Create an InputDefinition.
-
#make_input(value:, index:) ⇒ Object
Create an Input based on this definition, initializing it with the given value.
Methods included from Brut::Framework::FussyTypeEnforcement
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.
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
#max ⇒ Object (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 |
#maxlength ⇒ Object (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 |
#min ⇒ Object (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 |
#minlength ⇒ Object (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 |
#name ⇒ Object (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 |
#pattern ⇒ Object (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 |
#required ⇒ Object (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 |
#step ⇒ Object (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 |
#type ⇒ Object (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
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.
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 |