Display error in Play Framework 2

后端 未结 2 2241
挽巷
挽巷 2021-02-20 08:50

First of all I want to state that I think that the Play documentation for 2.0 is really, really bad.

I\'m looking for a way to place a validation error underneath a HTML

2条回答
  •  一向
    一向 (楼主)
    2021-02-20 09:47

    The answer from 2manyprojects works very well but you can do the same thing in the controller. It all depends on your preference and style.

    public static Result save() {
            Form
    boundForm = form.bindFromRequest(); if (boundForm.hasErrors()) { String errorMsg = ""; java.util.Map> errorsAll = boundForm.errors(); for (String field : errorsAll.keySet()) { errorMsg += field + " "; for (ValidationError error : errorsAll.get(field)) { errorMsg += error.message() + ", "; } } flash("error", "Please correct the following errors: " + errorMsg); return badRequest(detail.render(boundForm)); }

提交回复
热议问题