Skip to content

Commit

Permalink
Extract error_id
Browse files Browse the repository at this point in the history
To DRY up helper methods and error_summary partial.

Co-authored-by: James Mead <3169+floehopper@users.noreply.github.com>
  • Loading branch information
chrisroos and floehopper committed Oct 30, 2018
1 parent e94ec25 commit dfe9b4f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def govuk_fieldset_with_optional_error(journey, attribute)
error = journey.errors[attribute].first

options = { class: 'govuk-fieldset' }
options['aria-describedby'] = "#{attribute}-error" if error.present?
options['aria-describedby'] = error_id(attribute) if error.present?

content_tag :fieldset, options do
yield
Expand All @@ -41,7 +41,7 @@ def display_error(journey, attribute)
error = journey.errors[attribute].first
return if error.blank?

content_tag :span, id: "#{attribute}-error", class: 'govuk-error-message' do
content_tag :span, id: error_id(attribute), class: 'govuk-error-message' do
error
end
end
Expand All @@ -53,4 +53,8 @@ def css_classes_for_input(journey, attribute, extra_classes = [])
css_classes += ['govuk-input--error'] if error.present?
css_classes
end

def error_id(attribute)
"#{attribute}-error"
end
end
2 changes: 1 addition & 1 deletion app/views/journey/_error_summary.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ul class="govuk-list govuk-error-summary__list">
<% errors.keys.each do |attribute| %>
<li>
<a href="<%= "##{attribute}-error" %>"><%= errors[attribute].first %></a>
<a href="#<%= error_id(attribute) %>"><%= errors[attribute].first %></a>
</li>
<% end %>
</ul>
Expand Down

0 comments on commit dfe9b4f

Please sign in to comment.