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, #gui_element_id, #parent, #tag

Instance Method Summary collapse

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?, #untouch

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.



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

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

#disabled=(is_disabled) ⇒ Object



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

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.



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

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

#elementObject

Virtual method.

Raises:



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

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.



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

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

#hidden?Boolean

Whether this field element is hidden.

Returns:

  • (Boolean)


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

def hidden?
  (@hidden == true)
end

#hide!Object

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



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

def hide!
  @hidden = true
end

#invalid!Object

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



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

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

#invalid?Boolean

Whether this field element is marked as invalid.

Returns:

  • (Boolean)


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

def invalid? 
  @invalid == true
end

#optional!Object

Set field element to optional mode (default).



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

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.



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

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

#readonly=(is_readonly) ⇒ Object

Set :readonly flag (true | false).



185
186
187
188
189
190
191
192
# File 'lib/aurita-gui/form/form_field.rb', line 185

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)


181
182
183
# File 'lib/aurita-gui/form/form_field.rb', line 181

def readonly? 
  @readonly
end

#readonly_elementObject

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



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

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.



249
250
251
252
# File 'lib/aurita-gui/form/form_field.rb', line 249

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

#required?Boolean

Whether this field element is a required field.

Returns:

  • (Boolean)


259
260
261
# File 'lib/aurita-gui/form/form_field.rb', line 259

def required?
  (@required == true)
end

#show!Object

Remove :hidden flag from this element.



270
271
272
# File 'lib/aurita-gui/form/form_field.rb', line 270

def show!
  @hidden = false
end

#to_hidden_fieldObject

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



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

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.



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

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