I have some validation on input data which I really would prefer to handle in the controller code, because:
verifying
function in the form definition would lower code cohesion.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:
val f= Form(form.mapping, form.data,
Seq(new play.api.data.FormError("error.key", "my error")), form.value)
Apologies for the noise -- leaving it here in case someone else gets stuck as I did.
Shorter alternative:
val f= form.withError("error.key", "my error")), form.value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With