Module: CCS::FrontendHelpers::GovUKFrontend::ErrorMessage

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

Overview

GOV.UK Error Message

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

Instance Method Summary collapse

Instance Method Details

#govuk_error_message(error_message, attribute = nil, **options) ⇒ ActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Error message component

Parameters:

  • message (String)

    the message to be displayed

  • attribute (String, Symbol) (defaults to: nil)

    the attribute that has an error

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the error message HTML

  • :visually_hidden_text (String) — default: 'Error'

    visualy hidden text before the error message

  • :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_message.rb', line 22

def govuk_error_message(error_message, attribute = nil, **options)
  Components::GovUK::ErrorMessage.new(context: self, message: error_message, attribute: attribute, **options).render
end

#govuk_error_message_with_model(model, attribute, **options) ⇒ NilClass, ActiveSupport::SafeBuffer

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

Parameters:

  • model (ActiveModel)

    model that will be used to find the error message

  • attribute (String, Symbol)

    the attribute that has an error

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the error message HTML

  • :visually_hidden_text (String) — default: 'Error'

    visualy hidden text before the error message

  • :attributes (Hash) — default: {}

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

Returns:

  • (NilClass, ActiveSupport::SafeBuffer)

    if the error message is not on the model it will return nil, otherwise it returns the error message HTML



37
38
39
40
41
42
# File 'lib/ccs/frontend_helpers/govuk_frontend/error_message.rb', line 37

def govuk_error_message_with_model(model, attribute, **options)
  error_message = model.errors[attribute].first
  return unless error_message

  Components::GovUK::ErrorMessage.new(context: self, message: error_message, attribute: attribute, **options).render
end