[2.3.x-java] Form database validation and dependency injection

152 views
Skip to first unread message

Erol Merdanović

unread,
Nov 20, 2014, 9:27:29 AM11/20/14
to play-fr...@googlegroups.com
Hi

Instead of Ebean ORM I'm using Spring Data. It enables me to create repositories and inject them intro controllers and perform database operations. The problem that I have is, how to inject repository (or anything else) into form?

I have a situation where users enters an email and I want to validate if the email exists in the database or not. I think the proper way is to do this in the form. Currently I'm solving this in 2 ways:

1. creating an extra method validateEmail(UserRepository ...) and calling this method after I completed binding and validating form in controller. It works, but it's very clumsy, I can forget to add it, and it adds extra code to the controller.

2. By accessing repository with Play.current().global().getControllerInstance(UserRepository.class);. Again, it works but it's untestable and kinda hacky.

My question is - is there a proper way to do it? Or should I actually do this checking in a service and populate form errors in case of an error?

Julien Richard-Foy

unread,
Nov 20, 2014, 9:42:17 AM11/20/14
to play-fr...@googlegroups.com
Hi, I think that’s a limitation of the DI capabilities of 2.3. We should definitely keep your use case so that it is properly handled in 2.4 (I think the Form helper should create the form instance by using the application’s injector).

In the meanwhile, I think the extra validation method is fine.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
  Z E N G U L A R I T Y
––––––––––––––––––––––
Julien RICHARD-FOY - Programmer

56 rue Saint Lazare 75009 Paris
625 Market Street - San Francisco

Erol Merdanović

unread,
Nov 27, 2014, 2:12:39 AM11/27/14
to play-fr...@googlegroups.com
Thank you for your reply. I will keep checking 2.4 release and hopefully it will be integrated.

Erol Merdanović

unread,
Oct 2, 2015, 3:19:35 AM10/2/15
to play-framework
Anything new about this problem? Are there any ways to inject components into forms?

Ivan

unread,
Oct 13, 2015, 3:51:35 PM10/13/15
to play-framework
I have similar problem. I'm using Play 2.4, Spring DI and Spring Data Jpa. At the moment I have Constraints.Required annotation on some fields.  I'm sending errors to browser this way:
return badRequest(form.errorsAsJson());
Then I parse it with JQuery and highlight fields with errors.
But I need custom validation for some fields. I don't know how to use validate() method mentioned in Play documentation because I don't have access to DB from my model class. Should I add extra method for building errors list? I suppose in that case I have to manage all errors by myself, build JSON object and send it instead of using Form's errorsAsJson() method. Are there any other options?

Erol Merdanović

unread,
Oct 13, 2015, 4:24:46 PM10/13/15
to play-framework
@Ivan

If you need to inject into validators (repository for doing DB validation) then check https://groups.google.com/forum/#!topic/play-framework-dev/O9tE1leKOO0 . There is an example how to modify Form.class to enable injection (it only works when used with Spring).

I think the idea is that you handle all the validation inside your form. You can do this with validators, so you don't need to add any extra methods or manually build error json. I makes the code cleaner and form easier to build/validate.

PS: I think this could also work with older Play version that have Spring integration.

Ivan

unread,
Oct 13, 2015, 7:14:01 PM10/13/15
to play-framework

Thanks, Erol!
Information about custom validators is useful. But I need some custom validators which can use DB repositories.
If I understand correctly, for working injections I need to modify Form class and rebuild framework. Also, I have to do this after each framework update. That's not an ideal solution, but anyway thank you for this option. I hope they fix it in Play framework soon


вівторок, 13 жовтня 2015 р. 23:24:46 UTC+3 користувач Erol Merdanović написав:

Erol Merdanović

unread,
Oct 14, 2015, 2:47:17 AM10/14/15
to play-framework
No. I solved this with creating FormDI.class which is a copy/paste of Form.class with modifications. I just included it into the project. No need for recompile. 

If you check the history - https://github.com/playframework/playframework/commits/master/framework/src/play-java/src/main/java/play/data/Form.java, there weren't many changes in near past. I think for now this is the best way until it's integrated into Play (but this can take some time + will it have a support for DI with Spring?).

Ivan

unread,
Oct 22, 2015, 5:24:56 PM10/22/15
to play-framework
Hi Erol,
Thanks for your help. I tried to create FormDI.java class. But I don't know how to make it work with play bootstrap3 plugin https://github.com/adrianhurt/play-bootstrap3 .
The problem is that its helpers use Form.Field class and I don't know how to make it accept Field from FormDI

середа, 14 жовтня 2015 р. 09:47:17 UTC+3 користувач Erol Merdanović написав:

Erol Merdanović

unread,
Oct 23, 2015, 5:51:57 AM10/23/15
to play-framework
Hm, you are right.. I will ask again if there are some news about integrating FormDI into core.
Reply all
Reply to author
Forward
0 new messages