[2.0-java] Problem Passing Form - invalid type - play.data.Form vs. play.api.data.Form

1,114 views
Skip to first unread message

Vladimir Vlach

unread,
Jul 21, 2012, 5:39:20 AM7/21/12
to play-fr...@googlegroups.com
HI,

I am building form using samples in Eclipse & Java. I can't get my form to work properly due to invalid classes passed to views.

play.data.Form<JobBean> jobForm = form(JobBean.class); 

and view requires object using play.api.data.Form. How can I fix this?

// controller
public static Result add() {
play.data.Form<JobBean> jobForm = form(JobBean.class);
play.data.Form<JobBean> jb = jobForm.bindFromRequest();
// The method render(Form<JobBean>) in the type addJobForm is not applicable for the arguments (Form<JobBean>)
Html content = views.html.Jobs.addJobForm.render(jb); // error
return ok(content);
}

// view
@(taskForm: play.api.data.Form[models.JobBean])
@import views.html.helper._
@views.html.main("Todo list") {
}

Thanks!
Vlad

opensource21

unread,
Jul 21, 2012, 11:40:13 AM7/21/12
to play-fr...@googlegroups.com
As far as I remember the java Form is a subclass from the scala-form, so this shouldn't be the problem. Looks to me more to a reload problem of eclipse. So first question:
Does play complains that's wrong or which tool does it?

Niels

ra...@oddly.co

unread,
Mar 14, 2013, 3:07:38 AM3/14/13
to play-fr...@googlegroups.com
Vladimir Vlach,

ra...@oddly.co

unread,
Mar 14, 2013, 3:08:55 AM3/14/13
to play-fr...@googlegroups.com
Vladimir Vlach, Did you solved the problem... I m having the same issue please help.

Cheers, 
Rajiv Croos

Vladimir Vlach

unread,
Mar 14, 2013, 5:20:28 AM3/14/13
to play-fr...@googlegroups.com
Hi Rajiv,

MAKE SURE the Object type in your template is SAME Object passed by render. I think the issue was that the template was expecting different object and was caused by typo. The message is kind of confusing. Let me know if it helps. I can dig into my code how I did it.

Vlad

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/PUtnS-uAEKo/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

ra...@oddly.co

unread,
Mar 14, 2013, 7:12:44 AM3/14/13
to play-fr...@googlegroups.com
Thanks for the quick response.

index.scala.html...................................

@(form: Form[Seed])

@main("Welcome") {

    <script src="@routes.Assets.at("javascripts/index.min.js")" type="text/javascript"></script>
    @helper.form(action = routes.Application.addSeed) {
        @helper.inputText(form("name"))
        @helper.inputText(form("url"))
        <input type="submit" value="Add seed"/>
    }
    
    <ul id="seeds"></ul>
}

....................................................................................................

and in the controller (Application.java)

import play.core.j.PlayMagicForJava;
import play.data.Form;
import play.libs.Json;
import play.mvc.Result;
import services.SeedService;
import views.html.index;

@org.springframework.stereotype.Controller
public class Application{

    @Autowired
    private SeedService seedService;

    public Result index() {
        return play.mvc.Controller.ok(index.render(play.data.Form.form(Seed.class)));
    }

    public Result addSeed() {
        Form<Seed> form = play.data.Form.form(Seed.class).bindFromRequest();
        Seed seed = form.get();
        seedService.addSeed(seed);
        return play.mvc.Controller.redirect(controllers.routes.Application.index());
    }

    public Result listSeeds() {
        return play.mvc.Controller.ok(Json.toJson(seedService.getAllSeeds()));
    }
    
}
.................................................................................................................

I know the view use play.api.data.Form while Controller use play.data.Form. To use one data type in the both places is the issue as i dont know how to render the Fom without using play.data.Form.form as play.api.data.Form doesnt have a form() method. I m stuck here.

Thanks a lot again.

Rajiv

Robby Pelssers

unread,
Apr 13, 2014, 10:05:04 AM4/13/14
to play-fr...@googlegroups.com
I had the same issue.

It seems that the templates assume play.api.data.Form by default

So this will give issues:

@(proposalForm: Form[Proposal])

@import helper._

@main("New proposal") {
    @form(action = routes.MainController.submitProposal()) {

    }
}

Change it into

@(proposalForm: play.data.Form[Proposal])

@import helper._

@main("New proposal") {
    @form(action = routes.MainController.submitProposal()) {

    }
}

And if eclipse or intellij don't automatically regenerate the code for the view... do a clean and compile from the command line.

Hope that helps :)

Robby

Alberto Souza

unread,
Apr 13, 2014, 10:35:48 PM4/13/14
to play-fr...@googlegroups.com
I use form in views all the time with no problems. Form is not a subclass of Scala Form, as you can see here => https://github.com/playframework/playframework/blob/master/framework/src/play-java/src/main/java/play/data/Form.java. Just receive @(form:Form[Seed) and everything should work. If not, try to use play.data.Form, but I think you don't need.

Will Sargent

unread,
Apr 14, 2014, 10:32:33 AM4/14/14
to play-fr...@googlegroups.com
If you add

templatesImport += “play.data._" 

to your SBT file then I think that should let it pick up the Java package.

Will.

--
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.

Reply all
Reply to author
Forward
0 new messages