Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.3 scala forms - how to customise constraint messages

I've created form in play framework with constraints:

val voucherForm = Form(
  mapping(
    "voucherName" -> nonEmptyText,
    "voucherCode" -> optional(text(minLength = 6).verifying(pattern("""[a-zA-Z0-9]+""".r, error = "...")))     
  )(VoucherForm.apply)(VoucherForm.unapply)
)

when I display this form on a web page I have constraint messages (like Required, Minimum length: 6, constraint.pattern) shown near input boxes.

I want to customise this constraint messages per input field (i.e. two nonEmptyText constraints in same form will have different constraint message). How could I do it?

like image 343
kurochenko Avatar asked Dec 04 '25 02:12

kurochenko


1 Answers

Instead of using nonEmptyText, could you not use text, and put your custom message in the verifying, along the lines of:

val voucherForm = Form(
  mapping(
    "voucherName" -> text.verifying(
      "Please specify a voucher name", f => f.trim!=""),
...
like image 75
wwkudu Avatar answered Dec 05 '25 21:12

wwkudu