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
Decorator class to use for decorating the form content.
-
#element_map ⇒ Object
Hash mapping field names to Form_Field instances.
-
#elements ⇒ Object
Array of Form_Field instances includes in this form.
-
#field_decorator ⇒ Object
Field decorator class to use for decorating single fields.
-
#fields ⇒ Object
Return array of field names currently available for rendering.
-
#fieldsets ⇒ Object
readonly
Array of fieldsets in this form, ordered by appearance in the form.
-
#values ⇒ Object
Hash mapping form field names to their values.
Attributes inherited from Element
#attrib, #force_closing_tag, #gui_element_id, #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, #aurita_gui_element, #clear_floating, #css_classes, #find_by_dom_id, #get_content, #has_content?, #id, #id=, #inspect, #js_init_code, #length, #method_missing, #recurse, #remove_class, #set_content, #swap, #to_ary, #touch, #touched?, #type=, #untouch
Methods included from Marshal_Helper_Class_Methods
Methods included from Marshal_Helper
Constructor Details
#initialize(params = {}, &block) ⇒ Form
Returns a new instance of Form.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/aurita-gui/form.rb', line 340 def initialize(params={}, &block) @fields = params[:fields] @values = params[:values] @fields ||= [] @elements = [] @element_map = {} @fieldsets = {} @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
Decorator class to use for decorating the form content.
334 335 336 |
# File 'lib/aurita-gui/form.rb', line 334 def content_decorator @content_decorator end |
#element_map ⇒ Object
Hash mapping field names to Form_Field instances.
325 326 327 |
# File 'lib/aurita-gui/form.rb', line 325 def element_map @element_map end |
#elements ⇒ Object
Array of Form_Field instances includes in this form.
322 323 324 |
# File 'lib/aurita-gui/form.rb', line 322 def elements @elements end |
#field_decorator ⇒ Object
Field decorator class to use for decorating single fields.
331 332 333 |
# File 'lib/aurita-gui/form.rb', line 331 def field_decorator @field_decorator end |
#fields ⇒ Object
Return array of field names currently available for rendering.
319 320 321 |
# File 'lib/aurita-gui/form.rb', line 319 def fields @fields end |
#fieldsets ⇒ Object (readonly)
Array of fieldsets in this form, ordered by appearance in the form.
338 339 340 |
# File 'lib/aurita-gui/form.rb', line 338 def fieldsets @fieldsets end |
#values ⇒ Object
Hash mapping form field names to their values.
328 329 330 |
# File 'lib/aurita-gui/form.rb', line 328 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)
375 376 377 378 379 |
# File 'lib/aurita-gui/form.rb', line 375 def [](index) return @elements[index] if index.kind_of? Numeric return @element_map[index] if @element_map[index] return @element_map[index.to_s] end |
#[]=(index, form_field) ⇒ Object
Assign / overwrite field element with index form_index. TODO: FIX ME
383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/aurita-gui/form.rb', line 383 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.
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/aurita-gui/form.rb', line 416 def add(form_field_element) touch() if form_field_element.is_a?(Fieldset) then form_field_element.field_decorator = @field_decorator form_field_element.content_decorator = @content_decorator @element_map.update(form_field_element.element_map) @elements << form_field_element @fieldsets[form_field_element.name.to_s] = form_field_element else 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 end @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.
369 370 371 |
# File 'lib/aurita-gui/form.rb', line 369 def attributes @element_map end |
#content ⇒ Object
Return underlying HTML element instance (HTML.ul), without wrapping HTML.form element.
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/aurita-gui/form.rb', line 546 def content @content = [] if @title then @content << HTML.h1(:class => :form_title) { @title } end fields().each { |field| if field.is_a?(Hash) then # This is a fieldset field.each_pair { |fieldset_name, fieldset_fields| # Get Fieldset instance by name from @element_map: @content << HTML.li { @fieldsets[fieldset_name.to_s] } } else 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 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.
397 398 399 |
# File 'lib/aurita-gui/form.rb', line 397 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
536 537 538 539 540 541 542 |
# File 'lib/aurita-gui/form.rb', line 536 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
}
409 410 411 |
# File 'lib/aurita-gui/form.rb', line 409 def each(&block) @elements.each(&block) end |
#editable! ⇒ Object
Set all form elements to editable mode.
614 615 616 617 618 |
# File 'lib/aurita-gui/form.rb', line 614 def editable! elements.each { |e| e.editable! } end |
#element ⇒ Object
Render this form to an HTML.form instance. Wraps result of #content.
584 585 586 |
# File 'lib/aurita-gui/form.rb', line 584 def element HTML.form(@attrib) { content() } end |
#header_string ⇒ Object
597 598 599 |
# File 'lib/aurita-gui/form.rb', line 597 def header_string HTML.form(@attrib).string.gsub('</form>','') end |
#readonly! ⇒ Object
Set all form elements to readonly mode.
608 609 610 611 612 |
# File 'lib/aurita-gui/form.rb', line 608 def readonly! elements.each { |e| e.readonly! } end |
#string ⇒ Object Also known as: to_s
Render this form to a string
602 603 604 |
# File 'lib/aurita-gui/form.rb', line 602 def string element().to_s end |