Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class HowManyParkingSpacesOrGaragesIncludedInRentController @Inject()(howManyPar
form.bindFromRequest().fold(
formWithErrors => {
val formWithCorrectedErrors = formWithErrors.errors.head match {
case value if value.key.isEmpty && value.messages.contains("howManyParkingSpacesOrGaragesIncludedInRent.error.required") =>
case value if value.key.isEmpty &&
value.messages.contains("howManyParkingSpacesOrGaragesIncludedInRent.allFields.error.required") ||
value.messages.contains("howManyParkingSpacesOrGaragesIncludedInRent.error.required") =>
val uncoveredSpaces = value.copy(key = "uncoveredSpaces")
val coveredSpaces = value.copy(key = "coveredSpaces")
val garages = value.copy(key = "garages")
Expand All @@ -94,9 +96,9 @@ class HowManyParkingSpacesOrGaragesIncludedInRentController @Inject()(howManyPar
rentAmount =>
raldRepo.insertHowManyParkingSpacesOrGaragesIncludedInRent(
credId = CredId(request.credId.getOrElse("")),
uncoveredSpaces = rentAmount.uncoveredSpaces,
coveredSpaces = rentAmount.coveredSpaces,
garages = rentAmount.garages
uncoveredSpaces = if(rentAmount.uncoveredSpaces == -1) 0 else rentAmount.uncoveredSpaces,
coveredSpaces = if(rentAmount.coveredSpaces == -1) 0 else rentAmount.coveredSpaces,
garages = if(rentAmount.garages == -1) 0 else rentAmount.garages
)
Future.successful(Redirect(routes.CheckRentFreePeriodController.show.url))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ object HowManyParkingSpacesOrGaragesIncludedInRentForm extends CommonFormValidat
Constraint[A] =
Constraint((input: A) => {
val formData = input.asInstanceOf[HowManyParkingSpacesOrGaragesIncludedInRentForm]
val totalSpaces = Seq(formData.uncoveredSpaces, formData.coveredSpaces, formData.garages).sum
if (totalSpaces > 0)
Valid
else
Invalid(fieldRequired)
(formData.uncoveredSpaces, formData.coveredSpaces, formData.garages) match {
case (-1,-1,-1) => Invalid(allFieldsRequiredError)
case (0, 0, 0) => Invalid(fieldRequired)
case (_,_,_)=> Valid
}
})

val form: Form[HowManyParkingSpacesOrGaragesIncludedInRentForm] = {
Expand All @@ -67,19 +67,19 @@ object HowManyParkingSpacesOrGaragesIncludedInRentForm extends CommonFormValidat
text()
.transform[String](_.strip().replaceAll(",", ""), identity)
.verifying(regexp(wholePositiveNumberRegexp.pattern(), uncoveredSpacesWholeNumError))
).transform[Int](_.map(_.toInt).getOrElse(0), value => Some(value.toString))
).transform[Int](_.map(_.toInt).getOrElse(-1), value => Some(value.toString))
.verifying(maximumValue[Int](maxValue, uncoveredSpacesTooHighError)),
"coveredSpaces" -> optional(
text()
.transform[String](_.strip().replaceAll(",", ""), identity)
.verifying(regexp(wholePositiveNumberRegexp.pattern(), coveredSpacesWholeNumError))
).transform[Int](_.map(_.toInt).getOrElse(0), value => Some(value.toString))
).transform[Int](_.map(_.toInt).getOrElse(-1), value => Some(value.toString))
.verifying(maximumValue[Int](maxValue, coveredSpacesTooHighError)),
"garages" -> optional(
text()
.transform[String](_.strip().replaceAll(",", ""), identity)
.verifying(regexp(wholePositiveNumberRegexp.pattern(), garagesWholeNumError))
).transform[Int](_.map(_.toInt).getOrElse(0), value => Some(value.toString))
).transform[Int](_.map(_.toInt).getOrElse(-1), value => Some(value.toString))
.verifying(maximumValue[Int](maxValue, garagesTooHighError)),
)
(HowManyParkingSpacesOrGaragesIncludedInRentForm.apply)(HowManyParkingSpacesOrGaragesIncludedInRentForm.unapply)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class HowManyParkingSpacesOrGaragesIncludedInRentControllerSpec extends Controll
mockRequestWithoutProperty()
val fakePostRequest = FakeRequest(routes.HowManyParkingSpacesOrGaragesIncludedInRentController.submit)
.withFormUrlEncodedBody(
"uncoveredSpaces" -> "",
"uncoveredSpaces" -> "0",
"coveredSpaces" -> "0",
"garages" -> "0"
).withHeaders(HeaderNames.authorisation -> "Bearer 1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class HowManyParkingSpacesOrGaragesIncludedInRentFormSpec extends AnyWordSpec wi
val boundForm = HowManyParkingSpacesOrGaragesIncludedInRentForm.form.bind(data)

boundForm.hasErrors shouldBe true
boundForm.errors shouldBe List(FormError("", "howManyParkingSpacesOrGaragesIncludedInRent.error.required"))
boundForm.errors shouldBe List(FormError("", "howManyParkingSpacesOrGaragesIncludedInRent.allFields.error.required"))
}

"fail to bind when input fields are all 0" in {
Expand Down