java - Playframework ad-hoc form validation is executed but does not work as it supose -
i have login
class validate
method follow:
public static class login { /** customer. */ @manytoone @constraints.required public customer customer; /** password. */ public string password; public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public customer getcustomer() { return this.customer; } public void setcustomer(customer c) { this.customer = c; } /** * validate. * * @return string */ @transactional public string validate() { return "global error"; } }
code form binding:
form<login> filledloginform = form(login.class); filledloginform.bindfromrequest();
when validate form follow:
if (filledloginform.hasglobalerrors()) { return badrequest(views.html.login.render(filledloginform)); } else if (filledloginform.haserrors()) { return badrequest(views.html.login.render(filledloginform)); } else { return ok("ok"); }
view:
<input type="hidden" id="customer_id" name="customer.id" value="@customer.id" /> @inputpassword( loginform("password"), '_label -> "hasło", '_showconstraints -> false, '_showerrors -> false )
i ok page, there no error, in validate method i've declared it.
i use play 2.2.6
i suggest check explicitely in controller. like:
final string cid = form().bindfromrequest().get("customer.id"); if (filledloginform.hasglobalerrors() || cid==null || cid.equals("")) { // in case either there validation errors or provided customer.id not valid return badrequest(yourtemplatehere.render(filledloginform)); }
an alternative can try rewrite validate()
method bit , check there (assuming there id
field in customer
class):
public string validate() { return customer.id != null ? null : "oh no, customer id empty"; }
p.s. having public fields , setters/getters @ same time bit confusing - switch either public fields or make them private , use getters , setters
Comments
Post a Comment