Skip to content

Commit f45d5ed

Browse files
authored
Merge pull request #90 from hmrc/NGR-3025_Report-a-Change-First-Rent-Period_Highlight-2-missed-date-fields
NGR-3025 First Rent Period: Highlight 2 missed date fields
2 parents aee0caf + 7bb1537 commit f45d5ed

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

app/uk/gov/hmrc/ngrraldfrontend/controllers/ProvideDetailsOfFirstRentPeriodController.scala

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,12 @@ class ProvideDetailsOfFirstRentPeriodController @Inject()(view: ProvideDetailsOf
6666
.bindFromRequest()
6767
.fold(
6868
formWithErrors =>
69-
val correctedFormErrors = formWithErrors.errors.map { formError =>
70-
(formError.key, formError.messages) match
71-
case (key, messages) if messages.head.contains("provideDetailsOfFirstRentPeriod.startDate") =>
72-
setCorrectKey(formError, "provideDetailsOfFirstRentPeriod", "startDate")
73-
case (key, messages) if messages.head.contains("provideDetailsOfFirstRentPeriod.endDate") =>
74-
setCorrectKey(formError, "provideDetailsOfFirstRentPeriod", "endDate")
75-
case _ =>
76-
formError
77-
}
78-
val formWithCorrectedErrors = formWithErrors.copy(errors = correctedFormErrors)
7969
Future.successful(BadRequest(view(
8070
request.property.addressFull,
81-
formWithCorrectedErrors,
71+
formWithErrors,
8272
startDateInput,
8373
endDateInput,
84-
buildRadios(formWithCorrectedErrors, firstRentPeriodRadio(formWithCorrectedErrors, inputText)),
74+
buildRadios(formWithErrors, firstRentPeriodRadio(formWithErrors, inputText)),
8575
mode
8676
))),
8777
provideDetailsOfFirstRentPeriod =>

app/uk/gov/hmrc/ngrraldfrontend/models/forms/mappings/Mappings.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ trait Mappings extends CommonFormValidators with Formatters:
6161

6262
def conditionalMoney(errorKeyPrefix: String, requiredOnCondition: Map[String, String] => Boolean): FieldMapping[Option[BigDecimal]] =
6363
of(using ConditionalMoneyFormatter(errorKeyPrefix, requiredOnCondition))
64+
65+
def money(errorKeyPrefix: String): FieldMapping[Option[BigDecimal]] =
66+
of(using ConditionalMoneyFormatter(errorKeyPrefix, _ => true))

app/uk/gov/hmrc/ngrraldfrontend/views/components/DateTextFields.scala.html

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,26 @@
1818
@import uk.gov.hmrc.hmrcfrontend.views.html.components.implicits._
1919

2020
@this(govukDateInput: GovukDateInput)
21+
2122
@(form: Form[_], dateInput: DateInput)(implicit messages: Messages)
2223

24+
@formWithCorrectedErrors = @{
25+
val secondaryErrors = form.errors.flatMap { formError =>
26+
if(formError.messages.head.contains("AndMonth.required.error")) {
27+
val monthKey = formError.key.replace(".day", ".month")
28+
Seq(formError.copy(key = monthKey))
29+
} else if(formError.messages.head.contains("AndYear.required.error")) {
30+
val yearKey = formError.key.replace(".day", ".year").replace(".month", ".year")
31+
Seq(formError.copy(key = yearKey))
32+
} else Seq.empty
33+
}
34+
form.copy(errors = form.errors ++ secondaryErrors)
35+
}
36+
2337
@govukDateInput(DateInput(
2438
id = dateInput.id,
2539
namePrefix = None,
2640
fieldset = dateInput.fieldset,
2741
hint = dateInput.hint,
28-
errorMessage = form(dateInput.id).error.map(e => ErrorMessage.errorMessageWithDefaultStringsTranslated(content = Text(messages(e.message, e.args:_*))))
29-
).withDayMonthYearFormField(form(dateInput.id)))
42+
errorMessage = form(dateInput.id).error.map(e => ErrorMessage.errorMessageWithDefaultStringsTranslated(content = Text(messages(e.message, e.args: _*))))
43+
).withDayMonthYearFormField(formWithCorrectedErrors(dateInput.id)))

0 commit comments

Comments
 (0)