I have some validation on input data which I really would prefer to handle in the controller code, because:
- It only applies in very specific circumstances, so cluttering the
verifyingfunction in the form definition would lower code cohesion. - It produces collateral results which I need to use elsewhere in the controller.
What’s a clean way form producing a new Form like the one just bound with an additional (field or general) error message in the success branch of Form.fold?
To illustrate, I would like something like the (non-existent) Form.withError method I’m calling here:
val form= myForm.bindFromRequest
form.fold(
errors => BadRequest(view(errors))
{
case(data, button) =>
button match {
case Some("save") =>
val r= costlyFunction(data)
if (r.isOk) {
doSomethingWith(r)
Ok(...)
}
else {
val f= form.withError("my custom error")
BadRequest(view(f))
}
case ...
}
}
Found it myself:
Apologies for the noise — leaving it here in case someone else gets stuck as I did.