Class: Presenters::ErrorSummaryPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_design_system_formbuilder/presenters/error_summary.rb

Overview

This is the default presenter for GOVUKDesignSystemFormBuilder::Elements::ErrorSummary and is intended to be easily replaceable should you have specific requirements that aren't met here.

The basic behaviour is to always show the first error message. In Rails, error message order is determined by the order in which the validations run, but if you need to do any other transformation or concatenation, this is the place to do it.

Instance Method Summary collapse

Constructor Details

#initialize(error_messages) ⇒ ErrorSummaryPresenter

Returns a new instance of ErrorSummaryPresenter.

Examples:

Input format:

ErrorSummaryPresenter.new({ attribute_one: ["first error", "second error"], attribute_two: ["third error"] })

Parameters:

  • error_messages (Hash)

    the error message hash in a format that matches Rails' `object.errors.messages`, so the format should be:


14
15
16
# File 'lib/govuk_design_system_formbuilder/presenters/error_summary.rb', line 14

def initialize(error_messages)
  @error_messages = error_messages
end

Instance Method Details

#formatted_error_messagesArray<Array(Symbol, String)>, Array<Array(Symbol, String, String)>

Converts @error_messages into an array of argument arrays that will be passed into GOVUKDesignSystemFormBuilder::Elements::ErrorSummary#list_item.

Examples:

Output format given the input above:

[[:attribute_one, "first error"], [:attribute_two, "third error"]]

Output with hard-coded URLs

[[:attribute_one, "first error", "https://example.com/attribute-one"], [:attribute_two, "third error", "https://example.com/attribute-two"]]

Returns:

  • (Array<Array(Symbol, String)>)

    array of attribute and message arrays

  • (Array<Array(Symbol, String, String)>)

    array of attribute, message and URL arrays.


29
30
31
# File 'lib/govuk_design_system_formbuilder/presenters/error_summary.rb', line 29

def formatted_error_messages
  @error_messages.map { |attribute, messages| [attribute, messages.first] }
end