Class: CCS::Components::GovUK::ErrorMessage

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/error_message.rb

Overview

GOV.UK Error Message

This is used to generate the error message component from the GDS - Components - Error message

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the error message

{ class: 'govuk-error-message' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(message:, attribute:, **options) ⇒ ErrorMessage

Returns a new instance of ErrorMessage.

Parameters:

  • message (String)

    the message to be displayed

  • 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



33
34
35
36
37
38
39
40
41
# File 'lib/ccs/components/govuk/error_message.rb', line 33

def initialize(message:, attribute:, **options)
  super(**options)

  @options[:attributes][:id] ||= "#{attribute}-error" if attribute

  @message = message
  @attribute = attribute
  @visually_hidden_text = @options[:visually_hidden_text] || 'Error'
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Error message component

Returns:

  • (ActiveSupport::SafeBuffer)


47
48
49
50
51
52
53
54
55
# File 'lib/ccs/components/govuk/error_message.rb', line 47

def render
  tag.p(**options[:attributes]) do
    if visually_hidden_text.present?
      concat(tag.span("#{visually_hidden_text}:", class: 'govuk-visually-hidden'))
      concat(' ')
    end
    concat(message)
  end
end