Class: Formulary::HtmlForm

Inherits:
Object
  • Object
show all
Includes:
Labels
Defined in:
lib/formulary.rb,
lib/formulary/html_form.rb

Defined Under Namespace

Modules: Fields, Labels

Constant Summary collapse

FIELD_TYPES =
[]
FIELD_GROUP_TYPES =
[]
SINGULAR_FIELD_SELECTOR =
<<-EOS
  input[type!='submit'][type!='button'][type!='reset'][type!='image'][type!='radio'][type!='checkbox'],
  textarea,
  select
EOS
GROUPED_FIELD_SELECTOR =
<<-EOS
  input[type='radio'],
  input[type='checkbox']
EOS

Instance Method Summary collapse

Methods included from Labels

#label_for_field

Constructor Details

#initialize(markup) ⇒ HtmlForm

Returns a new instance of HtmlForm.



16
17
18
19
# File 'lib/formulary/html_form.rb', line 16

def initialize(markup)
  @markup = markup
  fields
end

Instance Method Details

#data_field_value(name, data_field) ⇒ Object



46
47
48
49
# File 'lib/formulary/html_form.rb', line 46

def data_field_value(name, data_field)
  field = find_field(name)
  field.blank? ? nil : field.get_value_from_data_field(data_field)
end

#errorsObject



35
36
37
38
39
# File 'lib/formulary/html_form.rb', line 35

def errors
  fields.each_with_object({}) do |field, hash|
    hash[field.name] = field.error unless field.valid?
  end
end

#is_hidden_field?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/formulary/html_form.rb', line 41

def is_hidden_field?(name)
  field = find_field(name)
  !!field.try(:is_hidden?)
end

#valid?(params) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/formulary/html_form.rb', line 21

def valid?(params)
  params.each do |key, value|
    if value.kind_of?(Hash)
      value.each do |nested_key, nested_value|
        set_field_value("#{key}[#{nested_key}]", nested_value)
      end
    else
      set_field_value(key, value)
    end
  end

  fields.all?(&:valid?)
end