Class: ForestAdminDatasourceCustomizer::DSL::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_datasource_customizer/dsl/builders/form_builder.rb

Overview

FormBuilder provides a fluent DSL for building action forms

Examples:

Basic form

form do
  field :email, type: :string, widget: 'TextInput'
  field :age, type: :number
  field :photo, type: :file

  page do
    field :address, type: :string
    field :city, type: :string
  end
end

Dynamic form with conditionals

form do
  field :amount, type: :number, required: true
  field :reason, type: :string,
    # You can use either form_value (DSL-style) or get_form_value (legacy)
    required: ->(ctx) { ctx.form_value(:amount).to_i > 1000 },
    if_condition: ->(ctx) { ctx.form_value(:amount).to_i > 500 }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormBuilder

Returns a new instance of FormBuilder.



30
31
32
# File 'lib/forest_admin_datasource_customizer/dsl/builders/form_builder.rb', line 30

def initialize
  @fields = []
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



28
29
30
# File 'lib/forest_admin_datasource_customizer/dsl/builders/form_builder.rb', line 28

def fields
  @fields
end

Instance Method Details

#field(name, type:, widget: nil, options: nil, readonly: nil, required: nil, default: nil, description: nil, placeholder: nil, if_condition: nil, enum_values: nil, collection_name: nil, &block) ⇒ Object

Add a field to the form

Parameters:

  • name (String, Symbol)

    field name

  • type (String, Symbol)

    field type (:string, :number, :boolean, :date, :file, etc.)

  • widget (String) (defaults to: nil)

    optional widget type

  • options (Array<Hash>, Proc) (defaults to: nil)

    options for dropdown/radio widgets (static or dynamic)

  • readonly (Boolean, Proc) (defaults to: nil)

    whether field is read-only (static or dynamic)

  • required (Boolean, Proc) (defaults to: nil)

    whether field is required (static or dynamic)

  • default (Object, Proc) (defaults to: nil)

    default value (static or dynamic)

  • description (String, Proc) (defaults to: nil)

    field description (static or dynamic)

  • placeholder (String, Proc) (defaults to: nil)

    placeholder text (static or dynamic)

  • if_condition (Proc) (defaults to: nil)

    condition to display the field (dynamic only)

  • enum_values (Array, Proc) (defaults to: nil)

    enum values for enum fields (static or dynamic)

  • collection_name (String, Proc) (defaults to: nil)

    target collection name (static or dynamic)

  • block (Proc)

    optional proc for computed values



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/forest_admin_datasource_customizer/dsl/builders/form_builder.rb', line 48

def field(name, type:, widget: nil, options: nil, readonly: nil, required: nil, default: nil,
          description: nil, placeholder: nil, if_condition: nil, enum_values: nil, collection_name: nil, &block)
  field_def = {
    label: name.to_s,
    type: normalize_type(type)
  }

  field_def[:widget] = widget if widget
  field_def[:options] = options if options
  field_def[:is_read_only] = readonly if readonly
  field_def[:is_required] = required if required
  field_def[:default_value] = default if default
  field_def[:description] = description if description
  field_def[:placeholder] = placeholder if placeholder
  field_def[:if_condition] = if_condition if if_condition
  field_def[:enum_values] = enum_values if enum_values
  field_def[:collection_name] = collection_name if collection_name
  field_def[:value] = block if block

  @fields << field_def
end

#html(content) ⇒ Object

Add a HTML block

Parameters:

  • content (String)

    HTML content



103
104
105
106
107
108
109
# File 'lib/forest_admin_datasource_customizer/dsl/builders/form_builder.rb', line 103

def html(content)
  @fields << {
    type: 'Layout',
    component: 'HtmlBlock',
    content: content
  }
end

#page(&block) ⇒ Object

Add a page layout to group fields

Parameters:

  • block (Proc)

    block containing nested fields



72
73
74
75
76
77
78
79
80
81
# File 'lib/forest_admin_datasource_customizer/dsl/builders/form_builder.rb', line 72

def page(&block)
  page_builder = FormBuilder.new
  page_builder.instance_eval(&block)

  @fields << {
    type: 'Layout',
    component: 'Page',
    elements: page_builder.fields
  }
end

#row(&block) ⇒ Object

Add a row layout to arrange fields horizontally

Parameters:

  • block (Proc)

    block containing nested fields



85
86
87
88
89
90
91
92
93
94
# File 'lib/forest_admin_datasource_customizer/dsl/builders/form_builder.rb', line 85

def row(&block)
  row_builder = FormBuilder.new
  row_builder.instance_eval(&block)

  @fields << {
    type: 'Layout',
    component: 'Row',
    fields: row_builder.fields
  }
end

#separatorObject

Add a separator



97
98
99
# File 'lib/forest_admin_datasource_customizer/dsl/builders/form_builder.rb', line 97

def separator
  @fields << { type: 'Layout', component: 'Separator' }
end