Skip to content

Commit a5ccef3

Browse files
refactoring month year error message logic
1 parent 8e5cba8 commit a5ccef3

File tree

7 files changed

+27
-43
lines changed

7 files changed

+27
-43
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import uk.gov.hmrc.ngrraldfrontend.models.forms.InterimRentSetByTheCourtForm.for
2929
import uk.gov.hmrc.ngrraldfrontend.navigation.Navigator
3030
import uk.gov.hmrc.ngrraldfrontend.pages.InterimSetByTheCourtPage
3131
import uk.gov.hmrc.ngrraldfrontend.repo.SessionRepository
32+
import uk.gov.hmrc.ngrraldfrontend.utils.DateKeyFinder
3233
import uk.gov.hmrc.ngrraldfrontend.views.html.InterimRentSetByTheCourtView
3334
import uk.gov.hmrc.ngrraldfrontend.views.html.components.InputText
3435
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController
@@ -43,7 +44,7 @@ class InterimRentSetByTheCourtController @Inject()(interimRentSetByTheCourtView:
4344
sessionRepository: SessionRepository,
4445
navigator: Navigator,
4546
mcc: MessagesControllerComponents)(implicit appConfig: AppConfig, ec: ExecutionContext)
46-
extends FrontendController(mcc) with I18nSupport {
47+
extends FrontendController(mcc) with I18nSupport with DateKeyFinder {
4748

4849
def generateInputText(form: Form[InterimRentSetByTheCourtForm], inputFieldName: String)(implicit messages: Messages): HtmlFormat.Appendable = {
4950
inputText(
@@ -80,13 +81,8 @@ class InterimRentSetByTheCourtController @Inject()(interimRentSetByTheCourtView:
8081
formWithErrors => {
8182
val correctedFormErrors = formWithErrors.errors.map { formError =>
8283
(formError.key, formError.messages) match
83-
case (key, messages) if messages.contains("interimRentSetByTheCourt.startDate.before.1900.error") ||
84-
messages.contains("interimRentSetByTheCourt.year.required.error") =>
85-
formError.copy(key = "date.year")
86-
case ("date", messages) if messages.contains("interimRentSetByTheCourt.year.format.error") =>
87-
formError.copy(key = "date.year")
88-
case ("date", messages) =>
89-
formError.copy(key = "date.month")
84+
case (key, messages) if messages.head.contains("interimRentSetByTheCourt.date") =>
85+
setCorrectKey(formError, "interimRentSetByTheCourt", "date")
9086
case _ =>
9187
formError
9288
}

app/uk/gov/hmrc/ngrraldfrontend/models/forms/CommonFormValidators.scala

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ trait CommonFormValidators {
9494
monthYearEmptyValidation(input.asInstanceOf[NGRMonthYear], errorKeys)
9595
)
9696

97-
protected def isMonthYearValid[A](monthError: String, yearError: String) : Constraint[A] =
97+
protected def isMonthYearValid[A](errorKey: String) : Constraint[A] =
9898
Constraint((input: A) =>
9999
val date = input.asInstanceOf[NGRMonthYear]
100-
monthYearValidation(date, monthError, yearError)
100+
monthYearValidation(date, errorKey)
101101
)
102102

103103
protected def isMonthYearAfter1900[A](errorKey: String): Constraint[A] =
@@ -146,8 +146,7 @@ trait CommonFormValidators {
146146
Invalid(errorKey)
147147
else
148148
Valid
149-
150-
149+
151150
private def monthYearAfter1900Validation(date: NGRMonthYear, errorKey: String) =
152151
val maybeYear = date.year.toIntOption
153152
maybeYear match {
@@ -165,16 +164,14 @@ trait CommonFormValidators {
165164
else
166165
Valid
167166

168-
protected def monthYearValidation(date: NGRMonthYear, monthError: String, yearError:String) = {
167+
private def monthYearValidation(date: NGRMonthYear, errorKey: String) = {
169168
val maybeMonth = date.month.toIntOption
170169
val maybeYear = date.year.toIntOption
171170
(maybeMonth, maybeYear) match {
172-
case (Some(month), Some(year)) if month > 0 && month <= 12 =>
171+
case (Some(month), Some(year)) if month > 0 && month <= 12 && year.toString.length == 4 =>
173172
Valid
174-
case (_, None) =>
175-
Invalid(yearError)
176173
case (_, _) =>
177-
Invalid(monthError)
174+
Invalid(errorKey)
178175
}
179176
}
180177
}

app/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtForm.scala

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ object InterimRentSetByTheCourtForm extends CommonFormValidators with MonthYearM
3737

3838
def unapply(interimRentSetByTheCourtForm: InterimRentSetByTheCourtForm): Option[(BigDecimal, NGRMonthYear)] = Some(interimRentSetByTheCourtForm.amount, interimRentSetByTheCourtForm.date)
3939

40-
private def errorKeys(whichDate: String): Map[DateErrorKeys, String] = Map(
41-
Required -> s"$whichDate.required.error",
42-
Month -> s"$whichDate.month.required.error",
43-
Year -> s"$whichDate.year.required.error"
44-
)
45-
4640
val form: Form[InterimRentSetByTheCourtForm] = Form(
4741
mapping(
4842
"interimAmount" -> text()
@@ -60,9 +54,9 @@ object InterimRentSetByTheCourtForm extends CommonFormValidators with MonthYearM
6054
"date" -> monthYearMapping
6155
.verifying(
6256
firstError(
63-
isMonthYearEmpty(errorKeys("interimRentSetByTheCourt")),
64-
isMonthYearValid(monthError = "interimRentSetByTheCourt.month.format.error", yearError = "interimRentSetByTheCourt.year.format.error"),
65-
isMonthYearAfter1900("interimRentSetByTheCourt.startDate.before.1900.error")
57+
isMonthYearEmpty(errorKeys("interimRentSetByTheCourt", "date")),
58+
isMonthYearValid("interimRentSetByTheCourt.date.invalid.error"),
59+
isMonthYearAfter1900("interimRentSetByTheCourt.date.before.1900.error")
6660
)
6761
),
6862
)(InterimRentSetByTheCourtForm.apply)(InterimRentSetByTheCourtForm.unapply)

app/uk/gov/hmrc/ngrraldfrontend/views/InterimRentSetByTheCourtView.scala.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
@layout(pageTitle = Some(messages("interimRentSetByTheCourt.title")), showBackLink = true, fullWidth = false) {
3535
@formHelper(action = uk.gov.hmrc.ngrraldfrontend.controllers.routes.InterimRentSetByTheCourtController.submit(mode), Symbol("autoComplete") -> "off") {
3636
@if(form.errors.nonEmpty) {
37-
@govukErrorSummary(ErrorSummaryViewModel(form))
37+
@govukErrorSummary(ErrorSummaryViewModel(form, Some("interimRentSetByTheCourt"), Some(Seq("date"))))
3838
}
3939
<span class="govuk-caption-m">@propertyAddress</span>
4040
<h1 class="govuk-heading-l">@messages("interimRentSetByTheCourt.title")</h1>

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
id = s"$id.month",
4343
classes = s"govuk-input--width-2${if(
4444
form(s"$id.month").hasErrors
45-
|| form.errors.exists(_.key.contains("date"))
46-
|| form.errors.exists(_.args.contains("month")))" govuk-input--error" else ""}",
45+
|| form.errors.exists(_.key.equals("date")))" govuk-input--error" else ""}",
4746
name = s"$id.month",
4847
label = Some(messages("date.month")),
4948
value = form(s"$id.month").value
@@ -52,8 +51,7 @@
5251
id = s"$id.year",
5352
classes = s"govuk-input--width-4${if(
5453
form(s"$id.year").hasErrors
55-
|| form.errors.exists(_.key.contains("date"))
56-
|| form.errors.exists(_.args.contains("year")))" govuk-input--error" else ""}",
54+
|| form.errors.exists(_.key.equals("date")))" govuk-input--error" else ""}",
5755
name = s"$id.year",
5856
label = Some(messages("date.year")),
5957
value = form(s"$id.year").value

conf/messages

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,11 @@ date.year = Year
364364
interimRentSetByTheCourt.interimAmount.required.error = Enter how much the interim rent was, in pounds
365365
interimRentSetByTheCourt.interimAmount.tooLarge.error = Interim rent must be £9,999,999.99 or less
366366
interimRentSetByTheCourt.interimAmount.format.error = Interim rent must be a number, like 50,000
367-
interimRentSetByTheCourt.month.format.error = Date your interim rent started must be a real date
368-
interimRentSetByTheCourt.year.format.error = Date your interim rent started must be a real date
369-
interimRentSetByTheCourt.required.error = Date you started paying the interim rent must include a month and year
370-
interimRentSetByTheCourt.month.required.error = Date you started paying the interim rent must include a month
371-
interimRentSetByTheCourt.year.required.error = Date you started paying the interim rent must include a year
372-
interimRentSetByTheCourt.startDate.before.1900.error = The date you started paying interim rent must be 1900 or after
367+
interimRentSetByTheCourt.date.invalid.error = Date your interim rent started must be a real date
368+
interimRentSetByTheCourt.date.required.error = Date you started paying the interim rent must include a month and year
369+
interimRentSetByTheCourt.date.month.required.error = Date you started paying the interim rent must include a month
370+
interimRentSetByTheCourt.date.year.required.error = Date you started paying the interim rent must include a year
371+
interimRentSetByTheCourt.date.before.1900.error = The date you started paying interim rent must be 1900 or after
373372

374373
#RentFreePeriod
375374
rentFreePeriod.title = Rent-free period

test/uk/gov/hmrc/ngrraldfrontend/models/forms/InterimRentSetByTheCourtFormSpec.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers {
157157
)
158158
val boundForm = InterimRentSetByTheCourtForm.form.bind(data)
159159

160-
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.month.format.error"))
160+
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.invalid.error"))
161161
}
162162
"fail to bind year before 1900 for year input field" in {
163163
val data = Map(
@@ -167,7 +167,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers {
167167
)
168168
val boundForm = InterimRentSetByTheCourtForm.form.bind(data)
169169

170-
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.startDate.before.1900.error"))
170+
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.before.1900.error"))
171171
}
172172

173173
"fail to bind missing month input" in {
@@ -178,7 +178,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers {
178178
)
179179
val boundForm = InterimRentSetByTheCourtForm.form.bind(data)
180180

181-
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.month.required.error"))
181+
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.month.required.error"))
182182
}
183183

184184
"fail to bind missing year input" in {
@@ -189,7 +189,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers {
189189
)
190190
val boundForm = InterimRentSetByTheCourtForm.form.bind(data)
191191

192-
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.year.required.error"))
192+
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.year.required.error"))
193193
}
194194

195195
"fail to bind non numeric format for month input" in {
@@ -200,7 +200,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers {
200200
)
201201
val boundForm = InterimRentSetByTheCourtForm.form.bind(data)
202202

203-
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.month.format.error"))
203+
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.invalid.error"))
204204
}
205205

206206
"fail to bind non numeric format for year input" in {
@@ -211,7 +211,7 @@ class InterimRentSetByTheCourtFormSpec extends AnyWordSpec with Matchers {
211211
)
212212
val boundForm = InterimRentSetByTheCourtForm.form.bind(data)
213213

214-
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.year.format.error"))
214+
boundForm.errors should contain(FormError("date", "interimRentSetByTheCourt.date.invalid.error"))
215215
}
216216

217217
}

0 commit comments

Comments
 (0)