Class: Superform::Rails::Field

Inherits:
Field show all
Defined in:
lib/superform/rails/field.rb

Overview

The Field class is designed to be extended to create custom forms. To override, in your subclass you may have something like this:

class MyForm < Superform::Rails::Form
  class MyLabel < Superform::Rails::Components::Label
    def view_template(&content)
      label(form: @field.dom.name, class: "text-bold", &content)
    end
  end

  class Field < Field
    def label(**attributes, &)
      MyLabel.new(self, **attributes, &)
    end

    def input(class: nil, **)
      super(class: ["input input-outline", grab(class:)])
    end
  end
end

Now all calls to label will have the text-bold class applied to it.

Instance Attribute Summary

Attributes inherited from Field

#dom, #object

Attributes inherited from Node

#key, #parent

Instance Method Summary collapse

Methods inherited from Field

add_method_to_kit, #assign, #collection, copy_field_methods_to_kit, #field, inherited, #initialize, #kit, method_added, #value

Methods inherited from Node

#initialize

Constructor Details

This class inherits a constructor from Superform::Field

Instance Method Details

#button(**attributes) ⇒ Object



28
29
30
# File 'lib/superform/rails/field.rb', line 28

def button(**attributes)
  Components::Button.new(field, **attributes)
end

#checkbox(index: nil, **attributes) ⇒ Object Also known as: check_box



36
37
38
# File 'lib/superform/rails/field.rb', line 36

def checkbox(index: nil, **attributes)
  Components::Checkbox.new(field, index:, **attributes)
end

#checkboxes(*options, **attributes, &block) ⇒ Object



159
160
161
162
# File 'lib/superform/rails/field.rb', line 159

def checkboxes(*options, **attributes, &block)
  options = enum_options if options.empty?
  Components::Checkboxes.new(field, options:, **attributes, &block)
end

#colorObject



138
139
140
# File 'lib/superform/rails/field.rb', line 138

def color(*, **)
  input(*, **, type: :color)
end

#datalist(*options, **attributes, &block) ⇒ Object



58
59
60
# File 'lib/superform/rails/field.rb', line 58

def datalist(*options, **attributes, &block)
  Components::Datalist.new(field, options:, **attributes, &block)
end

#dateObject



118
119
120
# File 'lib/superform/rails/field.rb', line 118

def date(*, **)
  input(*, **, type: :date)
end

#datetimeObject



126
127
128
# File 'lib/superform/rails/field.rb', line 126

def datetime(*, **)
  input(*, **, type: :"datetime-local")
end

#emailObject



97
98
99
# File 'lib/superform/rails/field.rb', line 97

def email(*, **)
  input(*, **, type: :email)
end

#errorsObject



62
63
64
# File 'lib/superform/rails/field.rb', line 62

def errors
  object.errors[key]
end

#fileObject



146
147
148
# File 'lib/superform/rails/field.rb', line 146

def file(*, **)
  input(*, **, type: :file)
end

#hiddenObject



89
90
91
# File 'lib/superform/rails/field.rb', line 89

def hidden(*, **)
  input(*, **, type: :hidden)
end

#human_attribute_nameObject



74
75
76
# File 'lib/superform/rails/field.rb', line 74

def human_attribute_name
  object.class.human_attribute_name key
end

#input(**attributes) ⇒ Object



32
33
34
# File 'lib/superform/rails/field.rb', line 32

def input(**attributes)
  Components::Input.new(field, **attributes)
end

#invalid?Boolean



66
67
68
# File 'lib/superform/rails/field.rb', line 66

def invalid?
  errors.any?
end

#label(**attributes) ⇒ Object



40
41
42
# File 'lib/superform/rails/field.rb', line 40

def label(**attributes, &)
  Components::Label.new(field, **attributes, &)
end

#monthObject



130
131
132
# File 'lib/superform/rails/field.rb', line 130

def month(*, **)
  input(*, **, type: :month)
end

#numberObject



110
111
112
# File 'lib/superform/rails/field.rb', line 110

def number(*, **)
  input(*, **, type: :number)
end

#passwordObject



93
94
95
# File 'lib/superform/rails/field.rb', line 93

def password(*, **)
  input(*, **, type: :password)
end

#radio(value, index: value, **attributes) ⇒ Object



150
151
152
# File 'lib/superform/rails/field.rb', line 150

def radio(value, index: value, **attributes)
  Components::Radio.new(field, value:, index:, **attributes)
end

#radios(*options, **attributes, &block) ⇒ Object



154
155
156
157
# File 'lib/superform/rails/field.rb', line 154

def radios(*options, **attributes, &block)
  options = enum_options if options.empty?
  Components::Radios.new(field, options:, **attributes, &block)
end

#rangeObject



114
115
116
# File 'lib/superform/rails/field.rb', line 114

def range(*, **)
  input(*, **, type: :range)
end

#searchObject



142
143
144
# File 'lib/superform/rails/field.rb', line 142

def search(*, **)
  input(*, **, type: :search)
end

#select(*options, multiple: false, **attributes) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/superform/rails/field.rb', line 48

def select(*options, multiple: false, **attributes, &)
  Components::Select.new(
    field,
    options:,
    multiple:,
    **attributes,
    &
  )
end

#telObject Also known as: phone



105
106
107
# File 'lib/superform/rails/field.rb', line 105

def tel(*, **)
  input(*, **, type: :tel)
end

#textObject

HTML5 input type convenience methods - clean API without _field suffix Examples:

field(:email).email(class: "form-input")
field(:age).number(min: 18, max: 99)
field(:birthday).date
field(:secret).hidden(value: "token123")
field(:gender).radio("male", id: "user_gender_male")


85
86
87
# File 'lib/superform/rails/field.rb', line 85

def text(*, **)
  input(*, **, type: :text)
end

#textarea(**attributes) ⇒ Object Also known as: text_area



44
45
46
# File 'lib/superform/rails/field.rb', line 44

def textarea(**attributes)
  Components::Textarea.new(field, **attributes)
end

#timeObject



122
123
124
# File 'lib/superform/rails/field.rb', line 122

def time(*, **)
  input(*, **, type: :time)
end

#titleObject



168
169
170
# File 'lib/superform/rails/field.rb', line 168

def title
  key.to_s.titleize
end

#urlObject



101
102
103
# File 'lib/superform/rails/field.rb', line 101

def url(*, **)
  input(*, **, type: :url)
end

#valid?Boolean



70
71
72
# File 'lib/superform/rails/field.rb', line 70

def valid?
  not invalid?
end

#weekObject



134
135
136
# File 'lib/superform/rails/field.rb', line 134

def week(*, **)
  input(*, **, type: :week)
end