Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How manually add errors in forms in play 2 framework

i'm trying addind some errors in forms but my code don't compile.
Expecially, seems that official play 2 api isn't correct.
we can see that errors() return an list of validationError
http://www.playframework.com/documentation/api/2.0/java/play/data/Form.Field.html#errors()

anyway if i try

 ValidationError e = new ValidationError("name", "user already exist",new ArrayList());
 filledForm.errors().add(e);

i got an error that method add don't exist.
I discovered that it is a hashmap but the follow code don't compile:

 filledForm.errors().put("name","s");

How add errors?? thanks

edit: solved thanks Julien Lafont

 ValidationError e = new ValidationError("name", "user already exist",new ArrayList());
 ArrayList<ValidationError> errors = new ArrayList<ValidationError>();
 errors.add(e);
 filledForm.errors().put("name",errors);
 return badRequest(loginForm.render(filledForm));
like image 727
user2054758 Avatar asked Feb 09 '13 15:02

user2054758


2 Answers

The short method is

filledForm.reject("name","user already exist");

return badRequest(loginForm.render(filledForm));

like image 148
Marco Avatar answered Nov 16 '22 23:11

Marco


You can use withError:

filledForm.withError("name", "user already exist")

You can add a global error too:

filledForm.withGlobalError("eneric error")

From source: https://github.com/playframework/playframework/blob/3bebfa7c1226a438a687ec9a0a3e5c23e5aefa09/framework/src/play/src/main/scala/play/api/data/Form.scala#L252

like image 11
yokomizor Avatar answered Nov 17 '22 00:11

yokomizor