playframework - Throw Global Validation error in Play -


i getting started play framework, right going through play's validation features. tried validate form fields shown in below view model class.

public class viewmodel {      @required(message = "please enter username")     public string username;     @required(message = "please enter password")     public string password;  } 

below simple demo ui same.

enter image description here

now, throw global error message when entered username , password incorrect when checked against database.

below code in controller action

        form<viewmodel> formdata = form.form(viewmodel.class).bindfromrequest();         if (formdata.haserrors()) {             logger.info("there server side validation errors in form..");             return badrequest(index.render(formdata));         } else {            if(notfoundindb){              //check against db              // db related code here assuming record not found in db                viewmodel user = formdata.get();              //formdata.error("invalid username , password");              return badrequest(index.render(formdata));            }else{              //success              return redirect("/nextaction");            }     } 

i have tried use formdata.error("invalid username , password");but not sure of this. in view template show global error message used following code.

@if(signin.hasglobalerrors) {   <p>        @signin.globalerror.message   </p> } 

how throw nice global error message controller action? using play 2.4.2 version.

you can use form.haserrors, form.globalerrors, or form("singleelementname").errors validation errors of form, e.g.

@if(form.haserrors) {   // form has error, global or individual? } @for(error <- form.globalerrors) {   // global errors, use @error.message message } @for(error <- form("singleelementname").errors) {   // individual error single form element, use @error.message } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -