Reputation: 1679
My form has two dates, but I want to validate that the second date occurs after the first date. Is there a concise way to do this using Play's Form verification?
Here is the form I am working with:
val policyForm: Form[PolicyForm] = Form(mapping(
"amount" -> of[Double].verifying("You need to enter in the amount of bitcoins", _ > 0),
"beginDate" -> date("yyyy-MM-dd").verifying("You cannot have a start date before today", dayLater _ ),
"endDate" -> date("yyyy-MM-dd").verifying("End date has be after the start date", { d =>
???
})
)(PolicyForm.apply _)(PolicyForm.unapply _))
Upvotes: 1
Views: 195
Reputation: 55569
You can't reference other form fields within the same mapping
until after they all successfully bind individually. That is, the constraint must be made on the outer mapping
.
val policyForm: Form[PolicyForm] = Form {
mapping(
"amount" -> of[Double].verifying(...),
"beginDate" -> date("yyyy-MM-dd").verifying(...),
"endDate" -> date("yyyy-MM-dd")
)(PolicyForm.apply _)(PolicyForm.unapply _).verifying("End date has be after the start date", policyForm =>
policyForm.beginDate.before(policyForm.endDate)
)
}
This assumes java.util.Date
, so replace with your own logic if you're using joda time, or something else.
Upvotes: 1