Class: Aurita::GUI::Form_Field
- 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.new(:onclick => 'submit();') { 'OK' }
= Form_Field.new(:name => :submit_button) { }
form.add()
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.
Direct Known Subclasses
Date_Field, File_Field, Hidden_Field, Input_Field, Options_Field, Selection_List_Option_Field, Textarea_Field, Time_Field
Instance Attribute Summary collapse
-
#data_type ⇒ Object
Returns the value of attribute data_type.
-
#form ⇒ Object
Returns the value of attribute form.
-
#hidden ⇒ Object
Returns the value of attribute hidden.
-
#hint ⇒ Object
Returns the value of attribute hint.
-
#invalid ⇒ Object
Returns the value of attribute invalid.
-
#label ⇒ Object
Returns the value of attribute label.
-
#required ⇒ Object
Returns the value of attribute required.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Attributes inherited from Element
#attrib, #force_closing_tag, #gui_element_id, #parent, #tag
Instance Method Summary collapse
-
#disable! ⇒ Object
Set field element to disabled mode.
- #disabled=(is_disabled) ⇒ Object
-
#editable! ⇒ Object
Set field element to editable mode.
-
#element ⇒ Object
Virtual method.
-
#enable! ⇒ Object
Set field element to enabled mode (default).
-
#hidden? ⇒ Boolean
Whether this field element is hidden.
-
#hide! ⇒ Object
Set hidden flag for this element.
-
#initialize(params, &block) ⇒ Form_Field
constructor
A new instance of Form_Field.
-
#invalid! ⇒ Object
Mark field element as invalid (e.g. missing value).
-
#invalid? ⇒ Boolean
Whether this field element is marked as invalid.
-
#optional! ⇒ Object
Set field element to optional mode (default).
-
#readonly! ⇒ Object
Set field element to readonly mode.
-
#readonly=(is_readonly) ⇒ Object
Set :readonly flag (true | false).
-
#readonly? ⇒ Boolean
Whether this field element is in readonly mode.
-
#readonly_element ⇒ Object
Render this form field element to a readonly element.
-
#required! ⇒ Object
Set field element to required mode.
-
#required? ⇒ Boolean
Whether this field element is a required field.
-
#show! ⇒ Object
Remove :hidden flag from this element.
-
#to_hidden_field ⇒ Object
Render this form field element to a Hidden_Field instance.
-
#to_s ⇒ Object
(also: #string)
Render this form field element to string.
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
Methods included from Marshal_Helper
Constructor Details
#initialize(params, &block) ⇒ Form_Field
Returns a new instance of Form_Field.
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_type ⇒ Object
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 |
#form ⇒ Object
Returns the value of attribute form.
97 98 99 |
# File 'lib/aurita-gui/form/form_field.rb', line 97 def form @form end |
#hidden ⇒ Object
Returns the value of attribute hidden.
97 98 99 |
# File 'lib/aurita-gui/form/form_field.rb', line 97 def hidden @hidden end |
#hint ⇒ Object
Returns the value of attribute hint.
97 98 99 |
# File 'lib/aurita-gui/form/form_field.rb', line 97 def hint @hint end |
#invalid ⇒ Object
Returns the value of attribute invalid.
97 98 99 |
# File 'lib/aurita-gui/form/form_field.rb', line 97 def invalid @invalid end |
#label ⇒ Object
Returns the value of attribute label.
97 98 99 |
# File 'lib/aurita-gui/form/form_field.rb', line 97 def label @label end |
#required ⇒ Object
Returns the value of attribute required.
97 98 99 |
# File 'lib/aurita-gui/form/form_field.rb', line 97 def required @required end |
#type ⇒ Object
Returns the value of attribute type.
97 98 99 |
# File 'lib/aurita-gui/form/form_field.rb', line 97 def type @type end |
#value ⇒ Object
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 |
#element ⇒ Object
Virtual method.
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.
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.
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.
181 182 183 |
# File 'lib/aurita-gui/form/form_field.rb', line 181 def readonly? @readonly end |
#readonly_element ⇒ Object
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.
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_field ⇒ Object
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_s ⇒ Object 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 |