Class: Playbook::PbForm::FormBuilder::FormFieldBuilder

Inherits:
Module
  • Object
show all
Defined in:
app/pb_kits/playbook/pb_form/form_builder/form_field_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(method_name, kit_name:) ⇒ FormFieldBuilder

Returns a new instance of FormFieldBuilder.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/pb_kits/playbook/pb_form/form_builder/form_field_builder.rb', line 7

def initialize(method_name, kit_name:)
  define_method method_name do |name, props: {}, **options, &block|
    props[:label] = @template.label(@object_name, name) if props[:label] == true
    options = Hash(options)

    options[:skip_default_ids] = false unless options.key?(:skip_default_ids)
    options[:required] = true if props[:required]
    options[:placeholder] = props[:placeholder] || ""

    if props.key?(:validation)
      validation = props[:validation]
      options[:pattern] = validation[:pattern] if validation[:pattern].present?
      options[:data] = { message: validation[:message] } if validation[:message].present?
    end

    input = super(name, **options, &block)

    @template.pb_rails(kit_name, props: props) do
      input
    end
  end
end