Validation Form Form 2.0

I follow the tutorial at http://www.playframework.org/documentation/2.0/JavaForms

I created the LoginForm.java class (instead of the User.class example from the example. Not for the class that is saved, just to store form values)

package domain; import static play.data.validation.Constraints.*; public class LoginForm { @Required public String email; public String password; } 

And in my controller I do (as an example), but I set the values ​​for blank lines to try the @Required annotation.

 Form<LoginForm> loginForm = form(LoginForm.class); Map<String,String> anyData = new HashMap(); anyData.put("email", ""); anyData.put("password", ""); //Faking a post LoginForm postedLoginForm = loginForm.bind(anyData).get(); if(loginForm.hasErrors()) { //Just for this test task, should have another error handling.. return ok("@Required annotation kicked in.."); } else { return ok("Got form values, email: " + postedLoginForm.email + " password: " + postedLoginForm.password); } 

But with:

 LoginForm postedLoginForm = loginForm.bind(anyData).get(); 

I get an Execution exception [[IllegalStateException: No value]]

Therefore, he never checks / never comes in

 if(loginForm.hasErrors()) 

Does anyone know why this is? If I set the values ​​as an example:

 Map<String,String> anyData = new HashMap(); anyData.put("email", " bob@gmail.com "); anyData.put("password", "secret"); 

Everything works, and I get a LoginForm object with the correct values. Should I catch the exception? You should not take care of this and set loginForm.hasErrors = true?

Thanks for any help!

+7
source share
2 answers

This is the expected behavior.

Note that you must use .get () on the form. After checking for errors.

 LoginForm preLoginForm = loginForm.bind(anyData); if(loginForm.hasErrors()) { //Just for this test task, should have another error handling.. return ok("@Required annotation kicked in.."); } LoginForm postedLoginForm = preLoginForm.get(); // ... Now use postedLoginForm 
+25
source

This seems to be a bug in the Play 2.0 environment. I was able to replicate the same problem locally.

I opened the ticket https://play.lighthouseapp.com/projects/82401-play-20/tickets/313 if you want to continue.

0
source

All Articles