Class: Glib::JsonUi::ViewBuilder::Fields::AbstractField Abstract
- Inherits:
-
View
- Object
- JsonUiElement
- View
- Glib::JsonUi::ViewBuilder::Fields::AbstractField
- Defined in:
- app/helpers/glib/json_ui/view_builder/fields.rb
Overview
Subclass and override #determine_value to customize value extraction from models
Base class for all field components.
AbstractField provides common functionality shared by all form fields including:
-
Automatic label and placeholder resolution from I18n
-
Model property binding with ‘prop` parameter
-
Client-side validation with automatic error messages
-
Form integration and dirty checking
-
Change event handlers
Direct Known Subclasses
CheckGroup, ChipGroup, CreditCard, Date, Datetime, DynamicGroup, DynamicSelect, Location, MultiUpload, Otp, Phone, RadioGroup, Rating, Select, Sign, StripeExternalAccount, StripeToken, Submit, Text, TimeZone, Upload
Instance Attribute Summary
Attributes inherited from JsonUiElement
Instance Method Summary collapse
- #autoValidate(autoValidate) ⇒ Object
- #context ⇒ Object
-
#created ⇒ Object
Override.
- #default_url_options ⇒ Object
-
#determine_value(context, prop) ⇒ Object
To be overridden.
- #disableDirtyCheck(value) ⇒ Object
- #hint(hint) ⇒ Object
- #hint_args(hint_args) ⇒ Object
- #label(label) ⇒ Object
- #label_args(label_args) ⇒ Object
- #name(name) ⇒ Object
- #placeholder(placeholder) ⇒ Object
- #placeholder_args(placeholder_args) ⇒ Object
- #prop(prop) ⇒ Object
-
#validation(validation) ⇒ Object
Validates field input according to specified rules.
- #value(value) ⇒ Object
Methods inherited from View
Methods inherited from JsonUiElement
Constructor Details
This class inherits a constructor from Glib::JsonUi::JsonUiElement
Instance Method Details
#autoValidate(autoValidate) ⇒ Object
125 126 127 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 125 def autoValidate(autoValidate) @autoValidate = autoValidate end |
#context ⇒ Object
179 180 181 182 183 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 179 def context form = page.current_form association = form.nested_associations.last @context ||= association || form end |
#created ⇒ Object
Override
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 134 def created form = page.current_form if @prop && form context.field_assert_respond_to(@prop) @name ||= context.field_name(@prop, @multiple || false) @label ||= context.field_label(@prop, @label_args || {}) @hint ||= context.hint_label(@prop, @hint_args || {}) @placeholder ||= context.placeholder_label(@prop, @placeholder_args || {}) if form._autoValidate && @autoValidate.nil? || !@autoValidate.nil? && @autoValidate @validation ||= context.field_validation(@prop) end if form.current_dynamic_group.nil? # This is not relevant inside a dynamic group @value ||= determine_value(context, @prop) end end if form.present? && !form.disable_dirty_check.nil? json.set! 'disableDirtyCheck', form.disable_dirty_check end if !@disable_dirty_check.nil? json.set! 'disableDirtyCheck', @disable_dirty_check end json.name @name json.value @value if @value json.label @label if @label json.hint @hint if @hint # In general, use hint over placeholder for the following reasons: # - Placeholder gets hidden when there is input which may be confusing # - Placeholder competes with label for space json.placeholder @placeholder if @placeholder json.validation @validation if @validation.present? end |
#default_url_options ⇒ Object
60 61 62 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 60 def { only_path: true } end |
#determine_value(context, prop) ⇒ Object
To be overridden
175 176 177 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 175 def determine_value(context, prop) context.field_value(prop) end |
#disableDirtyCheck(value) ⇒ Object
129 130 131 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 129 def disableDirtyCheck(value) @disable_dirty_check = value end |
#hint(hint) ⇒ Object
97 98 99 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 97 def hint(hint) @hint = hint end |
#hint_args(hint_args) ⇒ Object
101 102 103 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 101 def hint_args(hint_args) @hint_args = hint_args end |
#label(label) ⇒ Object
89 90 91 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 89 def label(label) @label = label end |
#label_args(label_args) ⇒ Object
93 94 95 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 93 def label_args(label_args) @label_args = label_args end |
#name(name) ⇒ Object
113 114 115 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 113 def name(name) @name = name if name != Glib::Value::DEFAULT end |
#placeholder(placeholder) ⇒ Object
105 106 107 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 105 def placeholder(placeholder) @placeholder = placeholder end |
#placeholder_args(placeholder_args) ⇒ Object
109 110 111 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 109 def placeholder_args(placeholder_args) @placeholder_args = placeholder_args end |
#prop(prop) ⇒ Object
121 122 123 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 121 def prop(prop) @prop = prop end |
#validation(validation) ⇒ Object
Supported validation types: presence, required, absence, acceptance, numericality, format, inclusion, exclusion, length
Validates field input according to specified rules.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 72 def validation(validation) return if validation.blank? # Validate that all keys are supported validation types supported_validations = i[presence required absence acceptance numericality format inclusion exclusion length] invalid_keys = validation.keys.map(&:to_sym) - supported_validations if invalid_keys.any? raise ArgumentError, "Unsupported validation type(s): #{invalid_keys.join(', ')}. Supported types: #{supported_validations.join(', ')}" end if validation[:format].present? context.cast_to_js_regex(validation[:format]) end json.validation validation end |
#value(value) ⇒ Object
117 118 119 |
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 117 def value(value) @value = value if value != Glib::Value::DEFAULT end |