Sha256: 37e27c576a9fc9f657cb97b669f9e57ca91f3af36f10c5b61a35c8641e838e03

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require_relative '../../components/govuk/back_link'

module CCS
  module FrontendHelpers
    module GovUKFrontend
      # = GOV.UK Error Message
      #
      # This helper is used for generating the error message component from the
      # {https://design-system.service.gov.uk/components/error-message GDS - Components - Error message}

      module ErrorMessage
        # Generates the HTML for the GOV.UK Error message component
        #
        # @param (see CCS::Components::GovUK::ErrorMessage#initialize)
        #
        # @option (see CCS::Components::GovUK::ErrorMessage#initialize)
        #
        # @return (see CCS::Components::GovUK::ErrorMessage#render)

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

        # Generates the HTML for the GOV.UK Error message component using the error messages in an ActiveModel
        #
        # @param model [ActiveModel] model that will be used to find the error message
        # @param attribute [String, Symbol] the attribute that has an error
        # @param options [Hash] options that will be used in customising the HTML
        #
        # @option (see CCS::Components::GovUK::ErrorMessage#initialize)
        #
        # @return [NilClass, ActiveSupport::SafeBuffer] if the error message is not on the model it will return nil,
        #                                               otherwise it returns the error message HTML

        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
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ccs-frontend_helpers-0.1.2 lib/ccs/frontend_helpers/govuk_frontend/error_message.rb
ccs-frontend_helpers-0.1.1 lib/ccs/frontend_helpers/govuk_frontend/error_message.rb
ccs-frontend_helpers-0.1.1.rc.1 lib/ccs/frontend_helpers/govuk_frontend/error_message.rb
ccs-frontend_helpers-0.1.0.rc.7 lib/ccs/frontend_helpers/govuk_frontend/error_message.rb
ccs-frontend_helpers-0.1.0.rc.6 lib/ccs/frontend_helpers/govuk_frontend/error_message.rb
ccs-frontend_helpers-0.1.0.rc.5 lib/ccs/frontend_helpers/govuk_frontend/error_message.rb
ccs-frontend_helpers-0.1.0.rc.4 lib/ccs/frontend_helpers/govuk_frontend/error_message.rb
ccs-frontend_helpers-0.1.0.rc.3 lib/ccs/frontend_helpers/govuk_frontend/error_message.rb