[2.0] Most efficient way to set a default value for a 'select' field in a form

1,496 views
Skip to first unread message

MG

unread,
Apr 4, 2012, 11:09:13 PM4/4/12
to play-fr...@googlegroups.com
Hi - I am trying to figure out what is the most efficient method to set a default value for just one select field in a big complex form. One approach is to use the fill method of a Form, but then I would have to initialize every field in that form. For a form that has a large number of fields, this does not seem to be an efficient approach. Is there any better method?

A related question: is there some way to specify the default value for a field while defining a Form structure? For example, for the following Form definition, how do I set the 'country' field to be "US" by default?

val userForm = Form(
  mapping(
    "name" -> text,
    "age" -> number,
"country" -> text
 )(User.apply)(User.unapply) )

Thanks,
MG

Ivan Meredith

unread,
Apr 4, 2012, 11:49:06 PM4/4/12
to play-fr...@googlegroups.com
Does this work for you?

val userForm = Form(
mapping(
"name" -> text,
"age" -> number,
"country" -> text
 )

).bind(Map("country" -> "US")

> --
> 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/-/ttN2Y-nPqxUJ.
> 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.

Ivan Meredith

unread,
Apr 4, 2012, 11:50:12 PM4/4/12
to play-fr...@googlegroups.com

MG

unread,
Apr 5, 2012, 2:25:35 PM4/5/12
to play-fr...@googlegroups.com
Ivan, thanks for the prompt response.

Unfortunately, this will not work for me since bind checks the constraints and flags error on the other fields that are not initialized. For bind to work, I will have to initialize all the fields that have constraints. Since the default initial value for some of the fields is not known, I cannot initialize all the fields.

-MG

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

MG

unread,
Apr 9, 2012, 3:20:08 PM4/9/12
to play-fr...@googlegroups.com
Reading the source code for the select helper, I came up with this solution:

                    signupForm("country").copy(value=signupForm("country").value.map {x=>Some(x)}.getOrElse(Some("US")))

If anyone knows a better solution, please let me know.

Thanks,
MG


On Thursday, April 5, 2012 11:25:35 AM UTC-7, MG wrote:
Ivan, thanks for the prompt response.

Unfortunately, this will not work for me since bind checks the constraints and flags error on the other fields that are not initialized. For bind to work, I will have to initialize all the fields that have constraints. Since the default initial value for some of the fields is not known, I cannot initialize all the fields.

-MG

On Wednesday, April 4, 2012 8:50:12 PM UTC-7, Ivan Meredith wrote:

virtualeyes

unread,
May 26, 2012, 10:17:29 AM5/26/12
to play-fr...@googlegroups.com
country.withDefault("US") sure would be a nice addition compared to ;-(

signupForm("country").copy(
value=signupForm("country").value.map {x=>Some(x)}.getOrElse(Some("US")))

Reply all
Reply to author
Forward
0 new messages