[2.0] Reverse routing query string

815 views
Skip to first unread message

Leonard Punt

unread,
Apr 16, 2012, 10:24:21 AM4/16/12
to play-fr...@googlegroups.com
Hi!

I have a route which gets parameters from the query string:
GET /report controllers.Reports.createReport(beginDate: LocalDate, endDate: LocalDate)

So a url would look like this:
report?beginDate=11-04-2012&endDate=13-04-2012

Now I want to create a form, with as method GET, which creates this report. But if I use the form.scala.html helper:

@form(routes.Reports.createReport(), 'id -> "form")

I get a compilation error:
not enough arguments for method createReport: (beginDate: org.joda.time.LocalDate, endDate: org.joda.time.LocalDate)play.api.mvc.Call. Unspecified value parameters beginDate, endDate.

I have a workaround, namely give the parameters a default value (?= new LocalDate).

But is this a bug? Because if the query string is empty it is still a valid route, isn't it? And it is not necessary that the reverse routes fills the query string, is it?

Thanks,
Leonard

Félix GV

unread,
Apr 16, 2012, 12:56:39 PM4/16/12
to play-fr...@googlegroups.com
I think you need to supply the arguments that you defined in your routes file when you do a call to routes.Controller.action().

Something like this:

@form(routes.Reports.createReport(thisIsYourBeginDate, thisIsYourEndDate), 'id -> "form")

...makes sense?

--
Félix


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/Dbd1H3bAGyYJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Guillaume Bort

unread,
Apr 16, 2012, 1:10:24 PM4/16/12
to play-fr...@googlegroups.com
For a form you shouldn't put these parameters into the route. You can
handle them using a standard Form. Since it is a form, the route
should work without these parameter to allow functional validation.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/Dbd1H3bAGyYJ.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

--
Guillaume Bort

Leonard Punt

unread,
Apr 16, 2012, 1:38:37 PM4/16/12
to play-fr...@googlegroups.com
@Guillaume
But that is just what I want to avoid. I don't want to create a new Java class for the options of every report. I think that passing the options (it are just a few options) in the query string and binding them to the method parameters is a beter solution, isn't it?

@Felix
What I want is a form like this:
<form action="/report" method="GET">
  <input type="date" name="startDate">
  <input type="date" name="endDate">
</form>

So that after the submit the url becomes: report?beginDate=<fieldValue>&endDate=<fieldValue>. But I have to supply the arguments, as you stated correctly. Which is not what I want, because the action of my form should not be: report?beginDate=11-04-2012&endDate=13-04-2012

--
Leonard

Kevin Bosman

unread,
Apr 16, 2012, 2:09:29 PM4/16/12
to play-fr...@googlegroups.com
> But that is just what I want to avoid. I don't want to create a new Java class for the options of every report. I think that passing the options (it are just a few options) in the query string and binding them to the method parameters is a beter solution, isn't it?

What Guillaume is referring to:

Define your route as follows:
GET /report controllers.Reports.createReport

Then in your controller, just use a DynamicForm to retrieve the values
- you don't need a backing class in this case.

Guillaume Bort

unread,
Apr 16, 2012, 4:06:55 PM4/16/12
to play-fr...@googlegroups.com
Without that you can't properly handle the form. What if your users
enter something wrong in the startDate field? Should we just ignore it
and put null into the variable? Surely not, you want proper form
validation, redisplaying the form with correct error messages.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/play-framework/-/Ah_fyT024oUJ.

Leonard Punt

unread,
Apr 17, 2012, 4:44:02 AM4/17/12
to play-fr...@googlegroups.com
@Kevin
Thanks, I wasn't aware of its existence. But Guillaume is right about validation, so I'll use the 'normal' Form class anyway.

@Guillaume
True. You're right. Hadn't really thought about validation yet. So I'll use the Form class. But just out of curiosity: I think form validation and binding is pretty nice and easy in Scala, but for Java it can become kind of clumsy, do you agree with that?

Leonard

Guillaume Bort

unread,
Apr 17, 2012, 10:23:02 AM4/17/12
to play-fr...@googlegroups.com
Unfortunately I see no other way to express the structure of your form with the related constraints. The current way: a simple class with JSR-303 annotation is probably the best we can achieve in Java for now. 
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/fqLtbOPr7ggJ.
Reply all
Reply to author
Forward
0 new messages