Class: Practical::Views::Form::FieldErrorsComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/practical/views/form/field_errors_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f:, object_method:, options:) ⇒ FieldErrorsComponent

Returns a new instance of FieldErrorsComponent.



6
7
8
9
10
# File 'app/components/practical/views/form/field_errors_component.rb', line 6

def initialize(f:, object_method:, options:)
  @f = f
  @object_method = object_method
  @options = options
end

Instance Attribute Details

#fObject (readonly)

Returns the value of attribute f.



4
5
6
# File 'app/components/practical/views/form/field_errors_component.rb', line 4

def f
  @f
end

#object_methodObject (readonly)

Returns the value of attribute object_method.



4
5
6
# File 'app/components/practical/views/form/field_errors_component.rb', line 4

def object_method
  @object_method
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'app/components/practical/views/form/field_errors_component.rb', line 4

def options
  @options
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/components/practical/views/form/field_errors_component.rb', line 12

def call
  id = f.field_errors_id(object_method)
  classes = ["error-section", "wa-callout", "wa-danger"]
  errors = f.errors_for(object_method)

  if errors.blank?
    classes << ["no-server-errors"]
    errors = []
  end

  finalized_options = mix({id: id, class: classes}, options)

  return label(object_method, nil, finalized_options) {
    render Practical::Views::Form::ErrorListComponent.new(errors: errors)
  }
end