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.

Form_Field can be used directly as a decorator for any instance of Element. This is useful in case you want to add GUI component to a form that is not derived from Form_Field itself. To do so, pass an Element instance to the constructor’s block. Note that in any case, a Form_Field instance requires the :name attribute, even if this doesn’t make sense at first glance when not adding a ‘real’ form field. This is necessary as it could not be accessed after adding it to the form otherwise.

Example:

button = Button.new(:onclick => 'submit();') { 'OK' }
button_field = Form_Field.new(:name => :submit_button) { button }
form.add(button_field)

In common cases, you won’t use Form_Field directly, but one of it’s derivates.

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!

You can also store an expected data type in an Form_Field. This is just for convenience for e.g. form generators. So far, Form_Field@data_type won’t be interpreted by any part of Aurita::GUI.

Instance Attribute Summary collapse

Attributes inherited from Element

#attrib, #force_closing_tag, #parent, #tag

Instance Method Summary collapse

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

Methods included from Marshal_Helper_Class_Methods

#marshal_load

Methods included from Marshal_Helper

#marshal_dump

Constructor Details

#initialize(params, &block) ⇒ Form_Field

Returns a new instance of Form_Field.

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/aurita-gui/form/form_field.rb', line 99

def initialize(params, &block)
  # @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]
  @data_type = params[:data_type]
  @invalid   = params[:invalid]
  @hint      = params[:hint]
  # 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.delete(:data_type)
  params.delete(:invalid)
  params.delete(:hint)
  params[:parent] = @form
  if block_given? then 
    @element = yield
    params[:content] = @element
  end
  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

#data_typeObject

Returns the value of attribute data_type.



97
98
99
# File 'lib/aurita-gui/form/form_field.rb', line 97

def data_type
  @data_type
end

#formObject

Returns the value of attribute form.



97
98
99
# File 'lib/aurita-gui/form/form_field.rb', line 97

def form
  @form
end

#hiddenObject

Returns the value of attribute hidden.



97
98
99
# File 'lib/aurita-gui/form/form_field.rb', line 97

def hidden
  @hidden
end

#hintObject

Returns the value of attribute hint.



97
98
99
# File 'lib/aurita-gui/form/form_field.rb', line 97

def hint
  @hint
end

#invalidObject

Returns the value of attribute invalid.



97
98
99
# File 'lib/aurita-gui/form/form_field.rb', line 97

def invalid
  @invalid
end

#labelObject

Returns the value of attribute label.



97
98
99
# File 'lib/aurita-gui/form/form_field.rb', line 97

def label
  @label
end

#requiredObject

Returns the value of attribute required.



97
98
99
# File 'lib/aurita-gui/form/form_field.rb', line 97

def required
  @required
end

#typeObject

Returns the value of attribute type.



97
98
99
# File 'lib/aurita-gui/form/form_field.rb', line 97

def type
  @type
end

#valueObject

Returns the value of attribute value.



97
98
99
# File 'lib/aurita-gui/form/form_field.rb', line 97

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.



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

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

#disabled=(is_disabled) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/aurita-gui/form/form_field.rb', line 234

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.



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

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

#elementObject

Virtual method.

Raises:



142
143
144
145
# File 'lib/aurita-gui/form/form_field.rb', line 142

def element
  raise Form_Error.new('Form_Field@element not set') unless @element
  @element
end

#enable!Object

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



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

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

#hidden?Boolean

Whether this field element is hidden.

Returns:

  • (Boolean)


281
282
283
# File 'lib/aurita-gui/form/form_field.rb', line 281

def hidden?
  (@hidden == true)
end

#hide!Object

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



273
274
275
# File 'lib/aurita-gui/form/form_field.rb', line 273

def hide!
  @hidden = true
end

#invalid!Object

Mark field element as invalid (e.g. missing value).



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

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

#invalid?Boolean

Whether this field element is marked as invalid.

Returns:

  • (Boolean)


207
208
209
# File 'lib/aurita-gui/form/form_field.rb', line 207

def invalid? 
  @invalid == true
end

#optional!Object

Set field element to optional mode (default).



261
262
263
264
# File 'lib/aurita-gui/form/form_field.rb', line 261

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.



183
184
185
186
# File 'lib/aurita-gui/form/form_field.rb', line 183

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

#readonly=(is_readonly) ⇒ Object

Set :readonly flag (true | false).



192
193
194
195
196
197
198
199
# File 'lib/aurita-gui/form/form_field.rb', line 192

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)


188
189
190
# File 'lib/aurita-gui/form/form_field.rb', line 188

def readonly? 
  @readonly
end

#readonly_elementObject

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



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

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.



256
257
258
259
# File 'lib/aurita-gui/form/form_field.rb', line 256

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

#required?Boolean

Whether this field element is a required field.

Returns:

  • (Boolean)


266
267
268
# File 'lib/aurita-gui/form/form_field.rb', line 266

def required?
  (@required == true)
end

#show!Object

Remove :hidden flag from this element.



277
278
279
# File 'lib/aurita-gui/form/form_field.rb', line 277

def show!
  @hidden = false
end

#to_hidden_fieldObject

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



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

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.



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

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