Class: Aurita::GUI::Form_Field

Inherits:
Element
  • Object
show all
Defined in:
lib/aurita-gui/form/form_field.rb

Overview

Class Form_Field is an abstract base class for built-in form fields (Input_Field, Hidden_Field, Radio_Field, Checkbox_Field, Select_Field, Textarea_Field) or any custom form field type. It is a wrapper for GUI::Element, extending it by parameters @name, @label and @value.

Usage:

i = Input_Field.new(:name => :description, 
                    :label => 'Description', 
                    :value => 'Lorem ipsum dolor')

Apart from this special attributes, Form_Field instances behave like any GUI::Element:

i.onclick = "alert('i have been clicked');"
i.class   = 'css_class'

To indicate a required form field, set the :required flag:

i = Input_Field.new(:required => true, :name => :description)

Or

i.required = true

A required field will always be rendered, even if it is not included in the form field settings. In this case, it is rendered as hidden field.

To force rendering a form element as hidden field, set the :hidden flag:

i = Input_Field.new(:hidden => true, :name => :hide_me)

Or

i.hidden = true

There is also a readonly render mode for form fields In readonly mode, a field element will be rendered as div element, containing the field value. This is useful for delete forms.

i = Input_Field.new(:readonly => true, :name => :description)

Or

i.readonly = true # or i.readonly!

And back to editable render mode:

i.redonly = false

Or

i.editable!

Instance Attribute Summary collapse

Attributes inherited from Element

#attrib, #content, #parent, #tag

Instance Method Summary collapse

Methods inherited from Element

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

Constructor Details

#initialize(params) ⇒ Form_Field

Returns a new instance of Form_Field.

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/aurita-gui/form/form_field.rb', line 72

def initialize(params)
  # @value  = params[:value]
  raise Form_Error.new('Must provide parameter :name for ' << self.class.to_s) unless params[:name]
  @form     = params[:parent]
  @form   ||= params[:form]
  @label    = params[:label]
  # Get value from params unless set by derived constructor: 
  @value    = params[:value] unless @value 
  @required = params[:required]
  @hidden   = params[:hidden]
  # Do not delete parameter value, as it is a 
  # standard for <input> elements. 
  # Field types not supporting the value attribute
  # (Textarea_Field, Option_Field, ...)
  # must delete it themselves. 
  @readonly = false
  params.delete(:form)
  params.delete(:parent)
  params.delete(:label)
  params.delete(:required)
  params.delete(:hidden)
  params[:parent] = @form
  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

#formObject

Returns the value of attribute form.



70
71
72
# File 'lib/aurita-gui/form/form_field.rb', line 70

def form
  @form
end

#hiddenObject

Returns the value of attribute hidden.



70
71
72
# File 'lib/aurita-gui/form/form_field.rb', line 70

def hidden
  @hidden
end

#labelObject

Returns the value of attribute label.



70
71
72
# File 'lib/aurita-gui/form/form_field.rb', line 70

def label
  @label
end

#requiredObject

Returns the value of attribute required.



70
71
72
# File 'lib/aurita-gui/form/form_field.rb', line 70

def required
  @required
end

#typeObject

Returns the value of attribute type.



70
71
72
# File 'lib/aurita-gui/form/form_field.rb', line 70

def type
  @type
end

#valueObject

Returns the value of attribute value.



70
71
72
# File 'lib/aurita-gui/form/form_field.rb', line 70

def value
  @value
end

Instance Method Details

#disable!Object

Set field element to disabled mode. See Aurita::GUI::Form for more information on rendering modes.



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

def disable! 
  @attrib[:disabled] = true
  add_class(:disabled)
end

#disabled=(is_disabled) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/aurita-gui/form/form_field.rb', line 177

def disabled=(is_disabled)
  @disabled = is_disabled
  if is_disabled then 
    add_class(:disabled) 
  else 
    remove_class(:disabled) 
  end
end

#editable!Object

Set field element to editable mode. See Aurita::GUI::Form for more information on rendering modes.



137
138
139
140
# File 'lib/aurita-gui/form/form_field.rb', line 137

def editable!
  @readonly = false
  remove_class(:readonly)
end

#elementObject

Virtual method.

Raises:



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

def element
  raise Form_Error.new('Cannot render abstract class Form_Field')
end

#enable!Object

Set field element to enabled mode (default). See Aurita::GUI::Form for more information on rendering modes.



173
174
175
176
# File 'lib/aurita-gui/form/form_field.rb', line 173

def enable! 
  @attrib.delete(:disabled)
  remove_class(:disabled)
end

#hidden?Boolean

Whether this field element is hidden.

Returns:

  • (Boolean)


224
225
226
# File 'lib/aurita-gui/form/form_field.rb', line 224

def hidden?
  (@hidden == true)
end

#hide!Object

Set hidden flag for this element. See Aurita::GUI::Form for more information on rendering modes.



216
217
218
# File 'lib/aurita-gui/form/form_field.rb', line 216

def hide!
  @hidden = true
end

#optional!Object

Set field element to optional mode (default).



204
205
206
207
# File 'lib/aurita-gui/form/form_field.rb', line 204

def optional!
  @required = false
  remove_class(:required)
end

#readonly!Object

Set field element to readonly mode. See Aurita::GUI::Form for more information on rendering modes.



145
146
147
148
# File 'lib/aurita-gui/form/form_field.rb', line 145

def readonly!
  @readonly = true
  add_class(:readonly)
end

#readonly=(is_readonly) ⇒ Object

Set :readonly flag (true | false).



154
155
156
157
158
159
160
161
# File 'lib/aurita-gui/form/form_field.rb', line 154

def readonly=(is_readonly)
  @readonly = is_readonly
  if is_readonly then 
    add_class(:readonly) 
  else 
    remove_class(:readonly) 
  end
end

#readonly?Boolean

Whether this field element is in readonly mode.

Returns:

  • (Boolean)


150
151
152
# File 'lib/aurita-gui/form/form_field.rb', line 150

def readonly? 
  @readonly
end

#readonly_elementObject

Render this form field element to a readonly element. Will not affect this element instance.



112
113
114
115
# File 'lib/aurita-gui/form/form_field.rb', line 112

def readonly_element
  # Todo: Add CSS classes 'readonly' and self.class
  HTML.div(@attrib) { @value }
end

#required!Object

Set field element to required mode. See Aurita::GUI::Form for more information on rendering modes.



199
200
201
202
# File 'lib/aurita-gui/form/form_field.rb', line 199

def required!
  @required = true
  add_class(:required)
end

#required?Boolean

Whether this field element is a required field.

Returns:

  • (Boolean)


209
210
211
# File 'lib/aurita-gui/form/form_field.rb', line 209

def required?
  (@required == true)
end

#show!Object

Remove :hidden flag from this element.



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

def show!
  @hidden = false
end

#to_hidden_fieldObject

Render this form field element to a Hidden_Field instance. Will not affect this element instance.



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

def to_hidden_field
  Hidden_Field.new(:type  => :hidden, 
                   :name  => @attrib[:name], 
                   :id    => dom_id.to_s, 
                   :value => @value)
end

#to_sObject Also known as: string

Render this form field element to string.



128
129
130
131
# File 'lib/aurita-gui/form/form_field.rb', line 128

def to_s
  return element().string unless @readonly
  return readonly_element().string
end