Class: Aurita::GUI::Form

Inherits:
Element show all
Defined in:
lib/aurita-gui/form.rb,
lib/aurita-gui/form/hidden_field.rb

Instance Attribute Summary collapse

Attributes inherited from Element

#attrib, #parent, #tag, #type

Instance Method Summary collapse

Methods inherited from Element

#+, #clear_floating, #dom_id, #dom_id=, #empty?, #id, #id=, #length, #method_missing, #to_ary

Constructor Details

#initialize(params, &block) ⇒ Form

Usage examples:

form = Form.new(:method   => :put  # default: :post
                :action   => '/where/to/send/form/'
                :onsubmit => "alert('submitting');") { 
  [ 
    Input_Field.new(:name => :description, :label => 'Description'), 
    Select_Field.new(:name => :category, :label => 'Select category')
  ]
}
textarea = GUI::Textarea.new(:name => :comment, :label => 'Comment')
form.add(textarea)

In case you want to override the form action (default: /aurita/dispatch), use :action_url:

Form.new(:action_url => '/where/to/send/form')

or

Form.action_url = '/where/to/send/form'


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/aurita-gui/form.rb', line 67

def initialize(params, &block)
  @action       = params[:action]
  @method       = params[:method]
  @fields       = params[:fields]
  @values       = params[:values]
  @method     ||= :post
  @fields     ||= []
  @elements     = []
  @element_map  = {}
  @values     ||= {}
  @title        = false
  @custom_fields = false
  if block_given? then
    yield.each { |e| add(e) }
  end
  params.delete(:fields)
  params.delete(:values)
  params[:tag]     = 'form'
  params[:content] = content()
  super(params)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Aurita::GUI::Element

Instance Attribute Details

#actionObject

Returns the value of attribute action.



44
45
46
# File 'lib/aurita-gui/form.rb', line 44

def action
  @action
end

#element_mapObject

Returns the value of attribute element_map.



44
45
46
# File 'lib/aurita-gui/form.rb', line 44

def element_map
  @element_map
end

#elementsObject

Returns the value of attribute elements.



44
45
46
# File 'lib/aurita-gui/form.rb', line 44

def elements
  @elements
end

#fieldsObject

Return array of field names currently available for rendering.



175
176
177
# File 'lib/aurita-gui/form.rb', line 175

def fields
  @fields
end

#methodObject

Returns the value of attribute method.



44
45
46
# File 'lib/aurita-gui/form.rb', line 44

def method
  @method
end

#targetObject

Returns the value of attribute target.



44
45
46
# File 'lib/aurita-gui/form.rb', line 44

def target
  @target
end

Instance Method Details

#[](index) ⇒ Object

Access form element by index or name (by index if parameter is of type Numeric, by name otherwhise)



98
99
100
101
# File 'lib/aurita-gui/form.rb', line 98

def [](index)
  return @elements[index] if index.kind_of? Numeric
  return @element_map[index.to_s] 
end

#[]=(index, form_field) ⇒ Object

Assign / overwrite field element with index form_index.



104
105
106
107
# File 'lib/aurita-gui/form.rb', line 104

def []=(index, form_field)
  @elements[index] = form_field
  @content = false # Invalidate
end

#add(form_field_element) ⇒ Object

Add form field element to this form.



128
129
130
131
132
133
134
135
# File 'lib/aurita-gui/form.rb', line 128

def add(form_field_element)
  if !form_field_element.dom_id then
    form_field_element.dom_id = form_field_element.name.to_s.gsub('.','_')
  end
  @element_map[form_field_element.name.to_s] = form_field_element
  @elements << form_field_element
  @content = false # Invalidate
end

#add_hidden(params) ⇒ Object Also known as: add_hidden_fields



36
37
38
39
40
# File 'lib/aurita-gui/form/hidden_field.rb', line 36

def add_hidden(params)
  params.each_pair { |k,v| 
    add(Hidden_Field.new(:name => k, :value => v))
  }
end

#attributesObject

Returns field element map. An element map maps field names to elements of this form.



92
93
94
# File 'lib/aurita-gui/form.rb', line 92

def attributes
  @element_map
end

#contentObject

Return underlying HTML element instance (HTML.ul), without wrapping HTML.form element.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/aurita-gui/form.rb', line 187

def content
  # TODO: Provide Fieldset instances
  @content = []
  if @title then
    @content << HTML.h1(:class => :form_title) { @title }
  end
  fields().each { |field|
    element = @element_map[field.to_s]
    if element then
      value   = @values[element.name.to_s] 
      value ||= @values[element.name.to_s.intern] 
      element.value = value if value
      if element.kind_of? Aurita::GUI::Hidden_Field then
        @content << element
      else
        @content << Form_Field_Wrapper.new(element)
      end
    end
  }
  cont = @content
  @content = HTML.ul(:class => :form_fields) { cont }
  return @content
end

#delete(field_name) ⇒ Object

Delete form field with name field_name from this form.



111
112
113
# File 'lib/aurita-gui/form.rb', line 111

def delete(field_name)
  @element_map.delete(field_name.to_s)
end

#each(&block) ⇒ Object

Iterate over form field elements. This would add a CSS class to all elements without a value:

form.each { |element| 
  element.class = 'missing' unless element.value
}


123
124
125
# File 'lib/aurita-gui/form.rb', line 123

def each(&block)
  @elements.each(&block)
end

#editable!Object

Set all form elements to editable mode.



231
232
233
234
235
# File 'lib/aurita-gui/form.rb', line 231

def editable! 
  elements.each { |e|
    e.editable! 
  }
end

#elementObject

Render this form to an HTML.form instance. Wraps result of #content.



213
214
215
216
# File 'lib/aurita-gui/form.rb', line 213

def element
  cont = content()
  HTML.form(@attrib) { cont }
end

#readonly!Object

Set all form elements to readonly mode.



167
168
169
170
171
# File 'lib/aurita-gui/form.rb', line 167

def readonly!
  @elements.each { |e|
    e.readonly!
  }
end

#stringObject Also known as: to_s

Render this form to a string



219
220
221
# File 'lib/aurita-gui/form.rb', line 219

def string
  element().to_s
end

#values=(value_hash = {}) ⇒ Object Also known as: set_values

Set field values for this form. Expects hash mapping field names to values. Example:

form.values = { :name => 'Foo', :description => 'Bar', :date => '20081012' }


161
162
163
# File 'lib/aurita-gui/form.rb', line 161

def values=(value_hash={})
  @values = value_hash
end