Checking domain objects using Play! framework

I want to check a domain object without automatically binding parameters to limit the properties that can be set by the client.

The next class (example from Play! Docs ) ...

public class User { @Required public String name; @Required @Min(0) public Integer age; } 

... usually checked as

 public static void hello(@Valid User user) { if(validation.hasErrors()) { params.flash(); validation.keep(); index(); } render(user); } 

But in this case, all user fields can be set by the client.

Is it possible to initiate verification of a domain object (rather than a "controller check") in Play! 1.2 explicitly?

 public static void hello(long id, String name) { User user = User.findById(id); user.name = name; user.validate(); // <-- I miss something like this if(validation.hasErrors()) { params.flash(); validation.keep(); index(); } render(user); } 
+4
source share
2 answers

You tried

 validation.valid(user); 
+4
source

You can add the @Required parameter to the name parameter and include it in:

 public static void hello(long id, @Required String name) { if(validation.hasErrors()) { params.flash(); validation.keep(); index(); } User user = User.findById(id); user.name = name; render(user); } 

You can extend the annotation to @Required (message = "key.to.i18n.message") for I18N purposes.

+1
source

All Articles