Class: Formalist::Elements::Field

Inherits:
Formalist::Element show all
Defined in:
lib/formalist/elements/field.rb

Instance Attribute Summary

Attributes inherited from Formalist::Element

#attributes, #children, #errors, #input, #name

Instance Method Summary collapse

Methods inherited from Formalist::Element

#==, build, fill, #initialize, #type

Methods included from Formalist::Element::ClassInterface

#attribute, #attributes_schema, #type

Constructor Details

This class inherits a constructor from Formalist::Element

Instance Method Details

#fill(input: {}, errors: {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/formalist/elements/field.rb', line 12

def fill(input: {}, errors: {})
  super(
    input: input[name],
    errors: errors[name].to_a,
  )
end

#to_astArray

Converts the field into an abstract syntax tree.

It takes the following format:

[:field, [params]]

With the following parameters:

  1. Field name
  2. Custom form element type (or :field otherwise)
  3. Associated form input data
  4. Error messages
  5. Form element attributes

Examples:

"email" field

field.to_ast
# => [:field, [
  :email,
  :field,
  "[email protected]",
  [],
  [:object, []],
]]

Returns:

  • (Array)

    the field as an abstract syntax tree.

See Also:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/formalist/elements/field.rb', line 48

def to_ast
  # errors looks like this
  # {:field_name => [["pages is missing", "another error message"], nil]}

  [:field, [
    name,
    type,
    input,
    errors,
    Element::Attributes.new(attributes).to_ast,
  ]]
end