Call form validation manually in Play Framework 2.x

I have a model with several constraint annotations. Is it possible to fill out the form with this model object, and then manually invoke the check. I am looking for something like this:

models.Photo photo = new models.Photo(); photo.loadValues(); Form<models.Photo> photoForm = new Form<models.Photo>(models.Photo.class); photoForm.fill(photo); 

And then I could do something like this:

 photoForm.validate(); #does not exist if (photoForm.hasErrors) { ... } 

Update : estmatic Answer works very well. To convert an object to a map, I use Beanutils:

 Map<String, String> photoMap = BeanUtils.describe(photo); 
+4
source share
1 answer

Instead of fill() you can use the bind() method and pass your preloaded values ​​to Map . This will trigger a check, for example, bindFromRequest .

 Map<String,String> values = loadValues(); Form<models.Photo> photoForm = form(models.Photo.class).bind(values); if (photoForm.hasErrors()){ ... } 
+2
source

All Articles