Module: CCS::FrontendHelpers::GovUKFrontend::ErrorSummary

Included in:
CCS::FrontendHelpers::GovUKFrontend
Defined in:
lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb

Overview

GOV.UK Error Summary

This helper is used for generating the error summary component from the GDS - Components - Error summary

Instance Method Summary collapse

Instance Method Details

#govuk_error_summary(title, error_summary_items = [], description = nil, **options) ⇒ ActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Error summary component

Parameters:

  • title (String)

    text to use for the heading of the error summary block

  • error_summary_items (Array<Hash>) (defaults to: [])

    the list of errors to include in the summary. See Item#initialize

  • description (String) (defaults to: nil)

    optional text to use for the description of the errors

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the error summary HTML

  • :attributes (Hash) — default: {}

    any additional attributes that will be added as part of the HTML

Returns:

  • (ActiveSupport::SafeBuffer)


22
23
24
# File 'lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb', line 22

def govuk_error_summary(title, error_summary_items = [], description = nil, **options)
  Components::GovUK::ErrorSummary.new(context: self, title: title, error_summary_items: error_summary_items, description: description, **options).render
end

#govuk_error_summary_with_model(model, title, description = nil, **options) ⇒ NilClass, ActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Error summary component using the error messages in an ActiveModel

Parameters:

  • model (ActiveModel)

    model that will be used to find the error messages to list

  • title (String)

    text to use for the heading of the error summary block

  • description (String) (defaults to: nil)

    (nil) optional text to use for the description of the errors

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the error summary HTML

  • :attributes (Hash) — default: {}

    any additional attributes that will be added as part of the HTML

Returns:

  • (NilClass, ActiveSupport::SafeBuffer)

    if error messages are not on the model it will return nil, otherwise it returns the error summary HTML.



38
39
40
41
42
43
44
# File 'lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb', line 38

def govuk_error_summary_with_model(model, title, description = nil, **options)
  return if model.errors.blank?

  error_summary_items = model.errors.map { |error| { text: error.message, href: "##{error.attribute}-error" } }

  Components::GovUK::ErrorSummary.new(context: self, title: title, error_summary_items: error_summary_items, description: description, **options).render
end