Sha256: 26a72840b83a8e31e273ae81acf247c180127d989ec2fd3c1c531027abc6c32a

Contents?: true

Size: 1.78 KB

Versions: 6

Compression:

Stored size: 1.78 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    class ErrorSummary < GOVUKDesignSystemFormBuilder::Base
      def initialize(builder, object_name, title)
        @builder = builder
        @object_name = object_name
        @title = title
      end

      def html
        return nil unless object_has_errors?

        @builder.content_tag('div', class: 'govuk-error-summary', **error_summary_attributes) do
          @builder.safe_join(
            [
              @builder.tag.h2(@title, id: error_summary_title_id, class: 'govuk-error-summary__title'),
              @builder.content_tag('div', class: 'govuk-error-summary__body') do
                @builder.content_tag('ul', class: 'govuk-list govuk-error-summary__list') do
                  @builder.safe_join(
                    @builder.object.errors.messages.map do |attribute, messages|
                      error_list_item(attribute, messages)
                    end
                  )
                end
              end
            ]
          )
        end
      end

    private

      def error_list_item(attribute, messages)
        @builder.content_tag('li') do
          @builder.tag.a(
            messages.join(', '),
            href: ['#', error_id(attribute)].join
          )
        end
      end

      def error_id(attribute)
        build_id('error', override_attribute_name: attribute)
      end

      def error_summary_title_id
        'error-summary-title'
      end

      def object_has_errors?
        @builder.object.errors.any?
      end

      def error_summary_attributes
        {
          tabindex: -1,
          role: 'alert',
          data: {
            module: 'error-summary'
          },
          aria: {
            labelledby: error_summary_title_id
          }
        }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-0.7.9 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-0.7.8 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-0.7.7 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-0.7.6 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-0.7.5 lib/govuk_design_system_formbuilder/elements/error_summary.rb
govuk_design_system_formbuilder-0.7.4 lib/govuk_design_system_formbuilder/elements/error_summary.rb