Sha256: 183a0182f7f149b936f57a03c9b06c47eabf27cd6dbd2f6cda745f013d240714

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

require "formalist/element"
require "formalist/types"

module Formalist
  class Elements
    class Field < Element
      permitted_children :none

      # @api private
      attr_reader :name

      attribute :label, Types::String
      attribute :hint, Types::String
      attribute :placeholder, Types::String
      attribute :inline, Types::Bool
      attribute :validation, Types::Validation

      # @api private
      attr_reader :predicates

      # @api private
      def initialize(*args, attributes, children, input, errors)
        super

        @name = Types::ElementName.(args.first)
        @input = input[@name] if input
        @errors = errors[@name].to_a
      end

      # 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
      #
      # @see Formalist::Element::Attributes#to_ast "Form element attributes" structure
      #
      # @example "email" field
      #   field.to_ast
      #   # => [:field, [
      #     :email,
      #     :field,
      #     "jane@doe.org",
      #     [],
      #     [:object, []],
      #   ]]
      #
      # @return [Array] the field as an abstract syntax tree.
      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
formalist-0.3.0 lib/formalist/elements/field.rb
formalist-0.2.3 lib/formalist/elements/field.rb