Class: Formulary::HtmlForm::Fields::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/formulary/html_form/fields/field.rb

Direct Known Subclasses

FieldGroup, Input, Select, Textarea

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_form, element) ⇒ Field

Returns a new instance of Field.



7
8
9
# File 'lib/formulary/html_form/fields/field.rb', line 7

def initialize(html_form, element)
  @html_form, @element = html_form, element
end

Class Method Details

.supports_required?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/formulary/html_form/fields/field.rb', line 3

def self.supports_required?
  false
end

Instance Method Details

#errorObject



23
24
25
# File 'lib/formulary/html_form/fields/field.rb', line 23

def error
  return "'#{label}' is required" if supports_required? && !presence_correct?
end

#labelObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/formulary/html_form/fields/field.rb', line 27

def label
  @label ||= \
    begin
      l = @html_form.label_for_field(name)

      if l.nil? then nil
      elsif l.is_a?(String) then l
      else l["fieldset"]
      end
    end
end

#nameObject



11
12
13
# File 'lib/formulary/html_form/fields/field.rb', line 11

def name
  @element.attributes["name"].value
end

#set_value(value) ⇒ Object



15
16
17
# File 'lib/formulary/html_form/fields/field.rb', line 15

def set_value(value)
  @value = value
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/formulary/html_form/fields/field.rb', line 19

def valid?
  supports_required? && presence_correct?
end