java - Thymeleaf validation: .hasErrors does not trigger -
i'm trying validate input values of form fieldset , make thymeleaf show error message when invalid inputs occur. somehow following thymeleaf command never gets resolved in right way:
<p th:if="${#fields.haserrors('itemid')}" th:errors="{*all}">name error</p>   more information environment won't hurt guess:
based on spring gs-guide "validating form input" implemented relevant controller method followed:
controller:
[...]         @requestmapping(value = "/explorer/optaresultrequest", method = requestmethod.post)     public string optaresultrequestpost(             httpservletrequest httprequest,             modelmap model,              @modelattribute("request") @valid somerequest userrequest,             final bindingresult bindingresult)     {          /* destroy existing "request" attribute: */         model.clear();          /* check invalid values: */         if (bindingresult.haserrors())         {             (int = 0; < bindingresult.getallerrors().size(); i++)             {                 log.info(bindingresult.getallerrors().get(i).getobjectname());                 log.info(bindingresult.getallerrors().get(i).getcode());                 log.info(bindingresult.getallerrors().get(i).tostring());             }              /* refill model new, empty "request" attribute. */             model.addattribute("currrequest", userrequest);             model.addattribute("request", new somerequest());              return "mypage";         }          // process valid request...      }   when submit invalid form input controller, runs if clause desired, way prints log.infos , returns same page again (mypage) - mentioned thymeleaf command does not show anything.
console:
 2015-07-22 16:22:28.151  info 6896 --- [nio-8080-exec-6] controller   :  request  2015-07-22 16:22:28.151  info 6896 --- [nio-8080-exec-6] controller   :  notempty  2015-07-22 16:22:28.151  info 6896 --- [nio-8080-exec-6] controller   :  field error in object 'request' on field 'itemid':         rejected value [];         codes [notempty.request.itemid,notempty.itemid,notempty.java.lang.string,notempty];         arguments [org.springframework.context.support.defaultmessagesourceresolvable:                    codes [request.itemid,itemid];                    arguments [];                    default message [itemid]];          default message [may not empty]   the mypage.html looks (simplified , shortened of cause):
mypage.html
<form action="#" th:object="${request}" th:action="@{/mypage}" method="post">     <fieldset>       <div>         <!--  itemid -->          <div>           <label>itemid</label>         </div>          <div>           <input type="text" th:field="*{itemid}" th:placeholder=           "${currrequest.itemid}" />         </div>          <p th:if="${#fields.haserrors('itemid')}" th:errors="{*all}">name error</p>       </div>     </fieldset>   </form>   so got 2 questions:
obviously: how display error messages?
i go deeper debugging - unfortunately not know how debug thymeleaf , (java) mvc (using sts) in general. managed include detailed loggings of everything.
but i.e. how track current/final value of thymeleaf variables #fields.haserrors()?
big in advance efforts!
all right, combination model.clear(); , model.addattribute("request", new somerequest()); reason.
i wasn't quite aware of thymeleaf engine gets invoked after return statement , can access new, empty request object, has no invalid attributes.
on other hand logger inside post-method accesses bindingresult-object , can print validation information independently of happens model , attributes inside method.
Comments
Post a Comment