[2.0.1-scala] boolean route parameter only accepts 0/1 values

622 views
Skip to first unread message

Richard Searle

unread,
Jun 29, 2012, 12:14:57 PM6/29/12
to play-fr...@googlegroups.com
This is inconvenient since Javascript will convert boolean to true/false.

 jsRoutes.controllers.Control.toggle($(this).prop('id'),$(this).prop('checked')?1:0).ajax({});

Note the ternary operator  to map to 0/1. 
This clutters the code and leads to silent failures when it is omitted.


Julien Richard-Foy

unread,
Jun 29, 2012, 1:59:23 PM6/29/12
to play-fr...@googlegroups.com
You should be able to use JavaScript booleans and the JavaScript reverse router will encode them to 0/1.
--
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/-/n_5KarfUo6UJ.
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.

Richard Searle

unread,
Jun 29, 2012, 2:14:23 PM6/29/12
to play-fr...@googlegroups.com
Without the ternary operator Play responds with:

For request 'POST /control/toggle/dl1/true' [Cannot parse parameter checked as Boolean: should be 0 or 1]

The route is

POST    /control/toggle/:name/:checked controllers.Control.toggle(name:String,checked:Boolean)



On Friday, June 29, 2012 12:59:23 PM UTC-5, Julien Richard-Foy wrote:
You should be able to use JavaScript booleans and the JavaScript reverse router will encode them to 0/1.

On 29 juin 2012, at 18:14, Richard Searle wrote:

This is inconvenient since Javascript will convert boolean to true/false.

 jsRoutes.controllers.Control.toggle($(this).prop('id'),$(this).prop('checked')?1:0).ajax({});

Note the ternary operator  to map to 0/1. 
This clutters the code and leads to silent failures when it is omitted.


--
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/-/n_5KarfUo6UJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

Richard Searle

unread,
Jun 29, 2012, 3:20:09 PM6/29/12
to play-fr...@googlegroups.com
The javascript router contains

_nS('controllers.Control'); _root.controllers.Control.toggle = 
      function(name,checked) {
      return _wA({method:"POST", url:"/control/toggle/" + (function(k,v) {return v})("name", name) + "/" + (function(k,v) {return v})("checked", checked)})
      }

Which does not contain any boolean to 0/1 mapping.

Julien Richard-Foy

unread,
Jun 29, 2012, 3:29:28 PM6/29/12
to play-fr...@googlegroups.com
Ok, this is a bug, but I think it has been solved in master.


To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/AA-AxmlmjeIJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

Richard Searle

unread,
Jun 29, 2012, 3:45:48 PM6/29/12
to play-fr...@googlegroups.com
Master contains code that provides special JS implementation. 
That is not in 2.0.2.

override def javascriptUnbind = """function(k,v){return k+'='+(v?1:0)}""" 


Thanks for the feedback.

Rotem

unread,
Aug 6, 2012, 9:38:38 AM8/6/12
to play-fr...@googlegroups.com
It's not just about javascript routing, it's wrong to consider boolean only as 1/0. If you define a REST api you want boolean parameters to accept true / false.
Is there a fix for this issue?

Julien Richard-Foy

unread,
Aug 6, 2012, 10:28:23 AM8/6/12
to play-fr...@googlegroups.com
Maybe we could accept both 0/1 and false/true as valid boolean
parameters and produce false/true with the reverse router. I think it
wouldn’t break anything. What do you guys think?
> --
> 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/-/qTMpbDprRjwJ.
>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.

Eric Jain

unread,
Aug 9, 2012, 8:03:31 PM8/9/12
to play-fr...@googlegroups.com
On Monday, August 6, 2012 7:28:23 AM UTC-7, Julien Richard-Foy wrote:
Maybe we could accept both 0/1 and false/true as valid boolean
parameters and produce false/true with the reverse router. I think it
wouldn’t break anything. What do you guys think?

Sounds like a good idea; I just ran into this as well...
 

Sébastien RENAULT

unread,
Sep 6, 2012, 3:59:14 AM9/6/12
to play-fr...@googlegroups.com
A pull request was merged for this : https://github.com/playframework/Play20/pull/415

Thanks to Julien :)

2012/8/10 Eric Jain <eric...@gmail.com>

--
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/-/uiTFbRnAot4J.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages