Skip to content

Commit b3ece40

Browse files
authored
Fix error messages (#63)
- Error messages were displaying as sentence fragments. I didn't look too deeply but my guess is `to_sentence` is part of an older Rails version. In Rails 8 looks like it should be errors.full_messages_for(attribute) - Note: This only displays the first error message for a field if there are multiple errors on that same field. I don't know design-wise whether we'd ever want to show more than one error message for a single field, seems like an odd use case. Maybe we can put a pin on that.
1 parent ad1d631 commit b3ece40

5 files changed

Lines changed: 6 additions & 4 deletions

File tree

app/helpers/flex/form_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def memorable_date(attribute, options = {})
219219
def field_error(attribute)
220220
return unless has_error?(attribute)
221221

222-
@template.content_tag(:span, object.errors[attribute].to_sentence, class: "usa-error-message")
222+
@template.content_tag(:span, object.errors.full_messages_for(attribute).first, class: "usa-error-message")
223223
end
224224

225225
def fieldset(legend, options = {}, &block)

config/locales/flex/en.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ en:
1616
boolean_true: "Yes"
1717
boolean_false: "No"
1818
date_picker_format: "Format: mm/dd/yyyy"
19+
memorable_date_hint: "For example: January 19 2000"
1920
optional: "Optional"
2021
select_month: "- Select -"
2122
tax_id_format: "For example, 123456789"
2223
errors:
2324
messages:
24-
invalid_date: "Invalid date"
25+
invalid_date: "is an invalid date"

config/locales/flex/es-US.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ es-US:
1616
boolean_true: ""
1717
boolean_false: "No"
1818
date_picker_format: "Formato: mm/dd/aaaa"
19+
memorable_date_hint: "Por ejemplo: Enero 19 2000"
1920
optional: "Opcional"
2021
select_month: "- Seleccionar -"
2122
tax_id_format: "Por ejemplo, 123456789"

spec/dummy/spec/lib/flex/attributes_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
expect(object.date_of_birth).to be_nil
5858
expect(object.date_of_birth_before_type_cast).to eq(input_hash)
5959
expect(object).not_to be_valid
60-
expect(object.errors["date_of_birth"]).to include("Invalid date")
60+
expect(object.errors.full_messages_for("date_of_birth")).to eq([ "Date of birth is an invalid date" ])
6161
end
6262
end
6363
end

spec/helpers/flex/form_builder_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@
333333
end
334334

335335
it 'displays the error message' do
336-
expect(result).to have_element(:span, text: 'Invalid date')
336+
expect(result).to have_element(:span, text: 'Date of birth is an invalid date')
337337
end
338338
end
339339
end

0 commit comments

Comments
 (0)