Class: Phlexi::Form::Base

Inherits:
HTML
  • Object
show all
Defined in:
lib/phlexi/form/base.rb

Overview

A form component for building flexible and customizable forms.

Examples:

Basic usage

Phlexi::Form(user, action: '/users', method: 'post') do
  render field(:name).placeholder("Name").input_tag
  render field(:email).placeholder("Email").input_tag
end

Defined Under Namespace

Classes: Builder, Errors, Namespace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, action: nil, method: nil, attributes: {}, **options) ⇒ Base

Initializes a new Form instance.

Parameters:

  • record (ActiveModel::Model, Symbol, String)

    The form’s associated record or key

  • action (String, nil) (defaults to: nil)

    The form’s action URL

  • method (String, nil) (defaults to: nil)

    The form’s HTTP method

  • attributes (Hash) (defaults to: {})

    Additional HTML attributes for the form tag

  • options (Hash)

    Additional options for form configuration

Options Hash (**options):

  • :class (String)

    CSS classes for the form

  • :namespace_klass (Class)

    Custom namespace class

  • :builder_klass (Class)

    Custom field builder class



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/phlexi/form/base.rb', line 49

def initialize(record, action: nil, method: nil, attributes: {}, **options)
  @form_action = action
  @form_method = method
  @dom_id = attributes.delete(:id)
  @attributes = attributes
  @namespace_klass = options.delete(:namespace_klass) || default_namespace_klass
  @builder_klass = options.delete(:builder_klass) || default_builder_klass
  @options = options

  initialize_object_and_key(record)
  initialize_namespace
  initialize_attributes
  super()
end

Instance Attribute Details

#keySymbol (readonly)

The form’s key, derived from the record or explicitly set

Returns:

  • (Symbol)

    the current value of key



20
21
22
# File 'lib/phlexi/form/base.rb', line 20

def key
  @key
end

#objectActiveModel::Model? (readonly)

The form’s associated object

Returns:

  • (ActiveModel::Model, nil)

    the current value of object



20
21
22
# File 'lib/phlexi/form/base.rb', line 20

def object
  @object
end

Class Method Details

.inline(&block) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
# File 'lib/phlexi/form/base.rb', line 27

def self.inline(*, **, &block)
  raise ArgumentError, "block is required" unless block

  new(*, **) do |f|
    f.instance_exec(&block)
  end
end

Instance Method Details

#error_messageObject



81
82
83
84
85
86
87
# File 'lib/phlexi/form/base.rb', line 81

def error_message
  lookups = []
  lookups << :"#{key}"
  lookups << :default_message
  lookups << options.fetch(:error_message) { "Please review the problems below" }
  I18n.t(lookups.shift, scope: :"phlexi_form.error_notification", default: lookups)
end

#errorsObject



89
90
91
# File 'lib/phlexi/form/base.rb', line 89

def errors
  @errors ||= options.fetch(:errors) { object.respond_to?(:errors) && object.errors.full_messages }
end

#extract_input(params, view_context: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/phlexi/form/base.rb', line 101

def extract_input(params, view_context: nil)
  params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
  params = {} unless params.is_a?(Hash)

  unless @_state
    raise ArgumentError, "view_context is required if Form has not been rendered" unless view_context
    view_context.render(self)
  end
  @namespace.extract_input(params)
end

#form_errorsObject



75
76
77
78
79
# File 'lib/phlexi/form/base.rb', line 75

def form_errors
  return unless errors.present?

  render self.class::Errors.new error_message, errors
end

#form_templatevoid

This method returns an undefined value.

Executes the form’s content block. Override this in subclasses to defie a static form.



97
98
99
# File 'lib/phlexi/form/base.rb', line 97

def form_template
  yield if block_given?
end

#view_templatevoid

This method returns an undefined value.

Renders the form template.



67
68
69
70
71
72
73
# File 'lib/phlexi/form/base.rb', line 67

def view_template(&)
  captured = capture { form_template(&) }
  form_tag do
    form_errors
    raw(safe(captured))
  end
end