[2.0.1-java] Unable to get play.data.Form.bindFromRequest() to work

7,125 views
Skip to first unread message

kvitso

unread,
May 5, 2012, 5:41:03 PM5/5/12
to play-fr...@googlegroups.com
The following code snippet descibes my problems with my first Play 2.0 application. I simply cannot manage to bind data from a form POST to the associated class. Form.data contains the values, but when I invoke bindFromRequest, the entity/bean is populated with null-values.

My controller class: http://pastebin.com/fmdfR4J8
My template: http://pastebin.com/k6jXTHvP
(these are nearly identical to the examples provided by Play (Zentasks).

I have even done this twice now, from a fresh app (in case there were other aspects that disturbed this process).

When i open this template and posts a username and a password to the controller, the System.out.println() debugs gives the following output:

[info] play - database [default] connected at jdbc:h2:mem:play
[info] play - Application started (Dev)
null
null
Form<Login>: Form(of=class controllers.Application$Login, data={email=mye...@address.com, password=mySecredPwd}, value=Some(Login [email=null, password=null]), errors={})
Login: Login [email=null, password=null]

So the values are obviously in the Form object, but is lost in the binding for some reason.

Also worth mentioning that if I have the validate method of the Login class as it is (ie. the validation fails), then play generated this errormessage to the screen:

[IllegalStateException: No value]

In ...\projects\sample\target\scala-2.9.1\src_managed\main\routes_routing.scala at line 60.

// @LINE:10
case controllers_Application_authenticate2(params) => {
   call { 
        invokeHandler(_root_.controllers.Application.authenticate(), HandlerDef(this, "controllers.Application", "authenticate", Nil))
   }
}

...with this corresponding stacktrace to the console:

[error] application -

! @6a9g04l6b - Internal server error, for request [POST /login] ->

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[IllegalStateException: No value]]
        at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [play_2.9.1.jar:2.0.1]
        at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [play_2.9.1.jar:2.0.1]
        at akka.actor.Actor$class.apply(Actor.scala:311) [akka-actor.jar:2.0.1]
        at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1.jar:2.0.1]
        at akka.actor.ActorCell.invoke(ActorCell.scala:619) [akka-actor.jar:2.0.1]
        at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:196) [akka-actor.jar:2.0.1]
Caused by: java.lang.IllegalStateException: No value
        at play.libs.F$None.get(F.java:217) ~[play_2.9.1.jar:2.0.1]
        at play.data.Form.get(Form.java:361) ~[play_2.9.1.jar:2.0.1]
        at controllers.Application.authenticate(Application.java:49) ~[classes/:2.0.1]
        at Routes$$anonfun$routes$1$$anonfun$apply$5$$anonfun$apply$6.apply(routes_routing.scala:60) ~[classes/:na]
        at Routes$$anonfun$routes$1$$anonfun$apply$5$$anonfun$apply$6.apply(routes_routing.scala:60) ~[classes/:na]
        at play.core.Router$HandlerInvoker$$anon$4$$anon$1.invocation(Router.scala:1086) ~[play_2.9.1.jar:2.0.1]

I get this with Play 2.0 and 2.0.1 (after upgrading). I am close to giving up 2.0.x now, and use 1.2 instead :(

Could anybody please help me with some explanation on what I am doing wrong here?

Kevin Bosman

unread,
May 6, 2012, 11:05:52 AM5/6/12
to play-fr...@googlegroups.com
Very strange. This works for me exactly as you have it in your
pastebins (removing the references to your User model obviously).
What do your application.conf and routes files look like?

[info] play - Application started (Dev)
test...@example.com
secretpassword
Form<Login>: Form(of=class controllers.Application$Login,
data={email=test...@example.com, password=secretpassword},
value=Some(Login [email=test...@example.com,
password=secretpassword]), errors={})
Login: Login [email=test...@example.com, password=secretpassword]

Kevin Bosman

unread,
May 6, 2012, 11:08:03 AM5/6/12
to play-fr...@googlegroups.com
> What do your application.conf and routes files look like?

FYI, I set my routes as follows:
GET / controllers.Application.login
POST / controllers.Application.authenticate

kvitso

unread,
May 6, 2012, 12:31:59 PM5/6/12
to play-fr...@googlegroups.com
My application.conf:  http://pastebin.com/J7pt4uis

So basically standard config.

Last night I discovered something strange. I cloned an earlier revision from my GIT repo, and it worked fine. Then I did some work on this version, but at some point this problem occured again. I then comitted (and pushed) the changes into the repo, and cloned it into a third directory. Now it worked again.

I also seem to have some issues with the changes in the scala templates being discovered by the java classes. If I modify the method signature of a template,then I often have to rename the template name (and back again, eg. home.scala.html -> homeNew.scala.html -> home.scala.html) for the API to be updated. I an using the latest version of Eclipse, and have enabled that the IDE should automatically update/refresh changed files (why this is not on by default is a mystery for me).

I have not verified that building from scratch (as when cloning my GIT repo) works all the time, but my immediate suspicion is that something is wrong with the generated classes. Are there any settings that could affect this...? OS (windows 7), encoding (UTF-8) etc...?

Kevin Bosman

unread,
May 6, 2012, 12:44:09 PM5/6/12
to play-fr...@googlegroups.com
It sounds to me like Eclipse is interfering with the bytecode enhancement again.

When it happens, have you tried doing a play clean-all and recompile
from the command line?

Check that you have disabled eclipse auto-build. I also find that
disabling refresh using native hooks or polling" helps tremendously
(depends on which version of Eclipse you're running)

kvitso

unread,
May 6, 2012, 3:18:21 PM5/6/12
to play-fr...@googlegroups.com
I think I actually do have audo-build enabled, could that be the reasons behind my issues?

I do not have access to my computer at the moment, but I will definately try to disable both those options (I suspect "Refresh using native hooks or polling" is also enabled).

Thanks for the help so far!

Kevin Bosman

unread,
May 6, 2012, 3:28:08 PM5/6/12
to play-fr...@googlegroups.com
It's just a suspicion, since ebean is involved, which requires
bytecode enhancement by the play build system, and eclipse is known to
try to recompile everything, which tends to override any enhancement
that has already been done by play itself.

If a fresh set of code seems to work, that normally indicates that
some kind of out-of-sync problem within the compiled classes is
evident. That's why I'd like you to try the play clean-all route as
well, since that cleans out all the compiled classes so play knows to
recompile them.

What confuses me however, is your static class should not
theoretically be getting enhanced by the EbeanPlugin, so I may be a
bit off the mark there...

Let us know how it goes...
Message has been deleted
Message has been deleted

kvitso

unread,
May 7, 2012, 5:43:38 PM5/7/12
to play-fr...@googlegroups.com
I am happy to announce that everything now works as expected! Both issues seem to be gone (has not happened tonight at least).

1)
General -> Workspace -> "Build automatically" turned off
General -> Workspace -> "Refresh using native hooks or polling" turned off

When i change the api of the templates (either method signatures or name) I have to compile or run from Play console, and then manually refresh the content in Package Explorer in Eclipse for it to pick up those changes. I will try re-enabling "Regresh using native hooks or polling" to see of that makes the IDE update automatically, without having the same issues again.

2)
I had also only upgraded Play itself, without setting 2.0.1 in plugins.sbt. I believe this fixed my other issue!

Thanks for the help

eson

unread,
May 14, 2012, 5:45:14 AM5/14/12
to play-fr...@googlegroups.com
hi,
   change Database column accept "default" value into "NULL".
by
Eson


On Sunday, 6 May 2012 03:11:03 UTC+5:30, kvitso wrote:
The following code snippet descibes my problems with my first Play 2.0 application. I simply cannot manage to bind data from a form POST to the associated class. Form.data contains the values, but when I invoke bindFromRequest, the entity/bean is populated with null-values.

My controller class: http://pastebin.com/fmdfR4J8
My template: http://pastebin.com/k6jXTHvP
(these are nearly identical to the examples provided by Play (Zentasks).

I have even done this twice now, from a fresh app (in case there were other aspects that disturbed this process).

When i open this template and posts a username and a password to the controller, the System.out.println() debugs gives the following output:

[info] play - database [default] connected at jdbc:h2:mem:play
[info] play - Application started (Dev)
null
null
Form<Login>: Form(of=class controllers.Application$Login, data={email=myemail@address.com, password=mySecredPwd}, value=Some(Login [email=null, password=null]), errors={})

Fabian Kessler

unread,
Jan 29, 2013, 2:15:11 PM1/29/13
to play-fr...@googlegroups.com
I was just hit by the same error, and spent an hour to solve it. This kind of error is what sucks the most energy.

My setup: play 2.0.4 (from the start, no upgrade) no orm, nothing fancy. java 7, windows 7 64bit. running as 'play debug', using idea, but never compiling in idea.

Just like reported: the value was submitted, it was there in the debugger, all fine, but just not bound to my form variable. So the "IllegalStateException: No value" was fired in form binding because of the @Constraints.Required annotation. 

public static class FooForm {
    @Constraints.Required
    public String foo;
}

What solved it was exiting, play clean, play clean-files, update, reload, compile, and run again.

Fabian

Fabian Kessler

unread,
Jan 30, 2013, 10:49:19 AM1/30/13
to play-fr...@googlegroups.com
yeah the validate() method is there to perform custom validation. you can return a string for an error message. that's all documented and works. 
i did not have a validate() method there.



On Tue, Jan 29, 2013 at 6:30 AM, Wladimir Tavares <tavares....@gmail.com> wrote:
Hi!

I had the same problem, it took me a while to unfold it. I guess I found the cause.  

For every class being "filled" with bindFromRequest() Play! will try to invoke from that class a method called validate. It's a second validation step after performing the regular play.data.validation package validations.

If your class validate method returns a value different from null, bindFromRequest() will return a Form object with a None object on the value field.

A None object will throw an IllegalStateException("No value") when it's method get is called.

You should avoid having a method validate from classes you are retrieving from a Form, unless you know what you are doing.

--
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/groups/opt_out.
 
 

Marc Baiges

unread,
Jan 31, 2013, 3:16:40 AM1/31/13
to play-fr...@googlegroups.com
Stuck in a similar place...

I'd like to bind the POST request to a Model that has some @Required fields, but some of these required values will be generated server side before persisting, such as a "creationDate" field. As the "creationDate" is not submitted by the front end, validation seems to be failing when bindingFromRequest.

Is there any way of bypassing this validation in 2.0.4?

thanks in advance,




El dimecres 30 de gener de 2013 7:49:19 UTC-8, Fabian Kessler va escriure:

Fabian Kessler

unread,
Jan 31, 2013, 9:44:59 AM1/31/13
to play-fr...@googlegroups.com
simply don't add the @Required annotation.
how about creating another class with composition, where your user-submitted post data object is passed in the constructor, and that has your additional features? something like that.
the form binding only works with what' really coming from the post.

(i'm quite new to play myself, but that's what i'd do.)

Marc Baiges

unread,
Jan 31, 2013, 12:36:47 PM1/31/13
to play-fr...@googlegroups.com
Thanks for your answer Fabian.

I'd rather keep the @Required in my models and benefit from what the ORM has to offer. I guess I'll have to deal with data transfer object as you suggest. 
But the promise of binding the request to the models in a flexible way sounded sexy!

thanks!


El dijous 31 de gener de 2013 6:44:59 UTC-8, Fabian Kessler va escriure:

Fabian Kessler

unread,
Jan 31, 2013, 1:04:05 PM1/31/13
to play-fr...@googlegroups.com
ah. to clarify, i'm not using orm, only the form binding.
hopefully someone else can give a better answer.

Fabian Kessler

unread,
Feb 20, 2013, 2:41:36 PM2/20/13
to play-fr...@googlegroups.com
just had this again. play clean solved it. still 2.0.4. was this fixed in 2.1?

Derek Rodger

unread,
Feb 28, 2013, 5:11:28 PM2/28/13
to play-fr...@googlegroups.com
I just ran into this today, using 2.1.0. Resolved it thanks to your advice to run play clean.

marti...@gmail.com

unread,
May 21, 2014, 8:01:11 PM5/21/14
to play-fr...@googlegroups.com
This is apparently still an issue in 2.2.3.  "play clean" resolved it for me.  Thanks much, Fabian.

Jaro

unread,
Jul 14, 2014, 3:34:45 PM7/14/14
to play-fr...@googlegroups.com
It took me a while to figure out the problem of "build automatically" from eclipse. Thanks for the post. This hint should be in the documentation though.

sudharsha...@cloudwick.com

unread,
Aug 15, 2016, 5:35:26 PM8/15/16
to play-framework
Hello Everyone,
I am facing exactly same issues and tried out all the option suggested above.
> Unchecked automatic build in eclipse
> Ran 'clean' in command lin

here is my Model :

public class MovieModel {
    public String row_key;
    public String column_family;
    public String column_name;
    public String column_value;

}


here is my view  movie.scala.html

@(myForm: Form[models.MovieModel])

@main("MovieModel") {

  @helper.form(action = routes.Movie.insertRecord()) {
    @helper.inputText(myForm("row_key"))
    @helper.inputText(myForm("column_family"))
    @helper.inputText(myForm("column_name"))
    @helper.inputText(myForm("column_value"))
    <input type="submit" class="btn btn-primary" value="Insert"/>
  }

}

Here is my controller :
public Result getMovie() {
      return ok(movie.render(form(MovieModel.class)));
    }

public Result insertRecord() {
Configuration config = null;
  Connection connection = null;
  Admin admin = null;
  String TABLE_NAME = "xxx";
 
  Form<MovieModel> formData = formFactory.form(MovieModel.class).bindFromRequest();
  MovieModel movie = formData.get();
 
  String row_key = movie.row_key;
  String column_family = movie.column_family;
  String column_name = movie.column_name;
  String column_value = movie.column_value;


I am getting null values into them. I have also tried explicitly writing getters and setters and tried to access these fields using getter, but no use. Can anyone help me in understanding, what's going on.

Thanks

K4G CybeX

unread,
Aug 18, 2017, 4:01:42 AM8/18/17
to Play Framework
I am getting this issue too.

After meddling with the conf/messages and attempting to customize @Constraints.Required messages, I got this issue in one of my forms:

Execution exception

[CompletionException: java.lang.IllegalStateException: Error(s) binding form: {"password":["This field is required"],"confirmPassword":["This field is required"],"email":["This field is required"]}]


However, previously this didn't occur in testing +/- 20min previously. No major changes were made.

I will try to clean-all and recompile, see if that helps at all
Reply all
Reply to author
Forward
0 new messages