Class: Aurita::GUI::Form
- Includes:
- Form_Field_Helper
- Defined in:
- lib/aurita-gui/form.rb,
lib/aurita-gui/form/hidden_field.rb,
lib/aurita-gui/form/template_helper.rb
Overview
Extend class Aurita::GUI::Form by helper methods.
Instance Attribute Summary collapse
-
#content_decorator ⇒ Object
Returns the value of attribute content_decorator.
-
#element_map ⇒ Object
Returns the value of attribute element_map.
-
#elements ⇒ Object
Returns the value of attribute elements.
-
#field_decorator ⇒ Object
Returns the value of attribute field_decorator.
-
#fields ⇒ Object
Return array of field names currently available for rendering.
-
#values ⇒ Object
Returns the value of attribute values.
Attributes inherited from Element
#attrib, #force_closing_tag, #parent, #tag
Instance Method Summary collapse
-
#[](index) ⇒ Object
Access form element by index or name (by index if parameter is of type Numeric, by name otherwhise).
-
#[]=(index, form_field) ⇒ Object
Assign / overwrite field element with index form_index.
-
#add(form_field_element) ⇒ Object
Add form field element to this form.
- #add_hidden(params) ⇒ Object (also: #add_hidden_fields)
-
#attributes ⇒ Object
Returns field element map.
-
#content ⇒ Object
Return underlying HTML element instance (HTML.ul), without wrapping HTML.form element.
-
#delete(field_name) ⇒ Object
Delete form field with name field_name from this form.
-
#delete_field(field_name) ⇒ Object
Remove field with name=field_name from list of elements to be rendered in the form.
-
#each(&block) ⇒ Object
Iterate over form field elements.
-
#editable! ⇒ Object
Set all form elements to editable mode.
-
#element ⇒ Object
Render this form to an HTML.form instance.
-
#header_string ⇒ Object
Returns opening tag of this form instance.
-
#initialize(params = {}, &block) ⇒ Form
constructor
A new instance of Form.
-
#readonly! ⇒ Object
Set all form elements to readonly mode.
-
#string ⇒ Object
(also: #to_s)
Render this form to a string.
Methods included from Form_Field_Helper
#boolean, #checkbox, #custom, #date, #datetime, #fieldset, #file, #hidden, #password, #radio, #select, #text, #textarea
Methods inherited from Element
#+, #<<, #add_class, #clear_floating, #css_classes, #find_by_dom_id, #get_content, #has_content?, #id, #id=, #method_missing, #recurse, #remove_class, #set_content, #swap, #to_ary, #type=
Methods included from Marshal_Helper_Class_Methods
Methods included from Marshal_Helper
Constructor Details
#initialize(params = {}, &block) ⇒ Form
Returns a new instance of Form.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/aurita-gui/form.rb', line 312 def initialize(params={}, &block) @fields = params[:fields] @values = params[:values] @fields ||= [] @elements = [] @element_map = {} @values ||= {} @title = false @custom_fields = false @field_decorator = Aurita::GUI::Form_Field_Wrapper @content_decorator = Aurita::GUI::Form_Content_Wrapper if block_given? then yield.each { |e| add(e) } end params.delete(:fields) params.delete(:values) params.delete(:title) params[:method] = 'POST' unless params[:method] params[:enctype] = 'multipart/form-data' unless params[:enctype] params[:tag] = 'form' params[:content] = content() params[:action] = @action 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
#content_decorator ⇒ Object
Returns the value of attribute content_decorator.
310 311 312 |
# File 'lib/aurita-gui/form.rb', line 310 def content_decorator @content_decorator end |
#element_map ⇒ Object
Returns the value of attribute element_map.
310 311 312 |
# File 'lib/aurita-gui/form.rb', line 310 def element_map @element_map end |
#elements ⇒ Object
Returns the value of attribute elements.
310 311 312 |
# File 'lib/aurita-gui/form.rb', line 310 def elements @elements end |
#field_decorator ⇒ Object
Returns the value of attribute field_decorator.
310 311 312 |
# File 'lib/aurita-gui/form.rb', line 310 def field_decorator @field_decorator end |
#fields ⇒ Object
Return array of field names currently available for rendering.
439 440 441 |
# File 'lib/aurita-gui/form.rb', line 439 def fields @fields end |
#values ⇒ Object
Returns the value of attribute values.
310 311 312 |
# File 'lib/aurita-gui/form.rb', line 310 def values @values end |
Instance Method Details
#[](index) ⇒ Object
Access form element by index or name (by index if parameter is of type Numeric, by name otherwhise)
346 347 348 349 |
# File 'lib/aurita-gui/form.rb', line 346 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. TODO: FIX ME
353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/aurita-gui/form.rb', line 353 def []=(index, form_field) @content = false # Invalidate if !index.kind_of? Numeric @element_map[index.to_s] = form_field @elements.collect { |e| e = form_field if e.name.to_s == index.to_s } else @elements[index] = form_field end end |
#add(form_field_element) ⇒ Object
Add form field element to this form. TODO: Should overwrite previous field element with same field name.
386 387 388 389 390 391 392 393 394 395 |
# File 'lib/aurita-gui/form.rb', line 386 def add(form_field_element) field_name = form_field_element.name.to_s form_field_element.value = @values[field_name] unless form_field_element.value.to_s != '' if !form_field_element.dom_id then form_field_element.dom_id = field_name.gsub('.','_') end @element_map[field_name] = form_field_element @elements << form_field_element @content = false # Invalidate end |
#add_hidden(params) ⇒ Object Also known as:
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 |
#attributes ⇒ Object
Returns field element map. An element map maps field names to elements of this form.
340 341 342 |
# File 'lib/aurita-gui/form.rb', line 340 def attributes @element_map end |
#content ⇒ Object
Return underlying HTML element instance (HTML.ul), without wrapping HTML.form element.
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/aurita-gui/form.rb', line 467 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 element = element.to_hidden_field() if element.hidden? if element.kind_of? Aurita::GUI::Hidden_Field then @content << element else @content << @field_decorator.new(element) end end } # Render required field as hidden field if not # included in form field config: @elements.each { |element| if !fields.include?(element.name.to_s) && element.required? then @content << element.to_hidden_field() end } fields_id = dom_id().to_s+'_fields' if dom_id() @content = @content_decorator.new(:id => fields_id) { @content } return @content end |
#delete(field_name) ⇒ Object
Delete form field with name field_name from this form.
367 368 369 |
# File 'lib/aurita-gui/form.rb', line 367 def delete(field_name) @element_map.delete(field_name.to_s) end |
#delete_field(field_name) ⇒ Object
Remove field with name=field_name from list of elements to be rendered in the form. The element will not be deleted from the form, so it can be enabled again using
form.fields << :field_name
457 458 459 460 461 462 463 |
# File 'lib/aurita-gui/form.rb', line 457 def delete_field(field_name) if field_name.kind_of? Numeric then @fields.delete_at(field_name) else @fields.delete(field_name.to_s) end 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
}
379 380 381 |
# File 'lib/aurita-gui/form.rb', line 379 def each(&block) @elements.each(&block) end |
#editable! ⇒ Object
Set all form elements to editable mode.
528 529 530 531 532 |
# File 'lib/aurita-gui/form.rb', line 528 def editable! elements.each { |e| e.editable! } end |
#element ⇒ Object
Render this form to an HTML.form instance. Wraps result of #content.
498 499 500 |
# File 'lib/aurita-gui/form.rb', line 498 def element HTML.form(@attrib) { content() } end |
#header_string ⇒ Object
511 512 513 |
# File 'lib/aurita-gui/form.rb', line 511 def header_string HTML.form(@attrib).string.gsub('</form>','') end |
#readonly! ⇒ Object
Set all form elements to readonly mode.
431 432 433 434 435 |
# File 'lib/aurita-gui/form.rb', line 431 def readonly! @elements.each { |e| e.readonly! } end |
#string ⇒ Object Also known as: to_s
Render this form to a string
516 517 518 |
# File 'lib/aurita-gui/form.rb', line 516 def string element().to_s end |