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) ⇒ 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

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, **)
  Components::GovUK::ErrorMessage.new(context: self, message: error_message, attribute: attribute, **).render
end

#govuk_error_message_with_model(model, attribute) ⇒ 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

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, **)
  error_message = model.errors[attribute].first
  return unless error_message

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