Class: Glib::JsonUi::ViewBuilder::Fields::AbstractField Abstract

Inherits:
View show all
Defined in:
app/helpers/glib/json_ui/view_builder/fields.rb

Overview

This class is abstract.

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

Instance Attribute Summary

Attributes inherited from JsonUiElement

#json, #page

Instance Method Summary collapse

Methods inherited from View

component_name

Methods inherited from JsonUiElement

#initialize, #props

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

#contextObject



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

#createdObject

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_optionsObject



60
61
62
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 60

def default_url_options
  { 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

Note:

Supported validation types: presence, required, absence, acceptance, numericality, format, inclusion, exclusion, length

Validates field input according to specified rules.

Examples:

validation presence: { message: 'Required' }
validation format: { with: /\A\d+\z/, message: 'Must be a number' }
validation length: { minimum: 3, maximum: 20, message: { too_short: 'Too short', too_long: 'Too long' } }

Parameters:

  • validation (Hash)

    Hash of validation types and their options

See Also:

  • Frontend validation implementation


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