Scala Play framework: Binding form parameters to hidden fields
Asked Answered
T

2

7

I am working with Play 2.0.4 and have the following form in my scala template.

@fieldGroup(field: Field, className: String = "field") = {
<div class="twipsies well @className">

    <a class="removeField btn danger pull-right">Remove Field</a>

    @inputText( // <=== I need a hidden input field here
        field("id")
    )

    @inputText(
        field("name"),
        '_label -> "Name",
        '_help -> "Use lower case, starts with an alphabet can contain numbers and underscores."
    )
}

I need a few hidden fields in my forms, how do I bind it to the server side Form component? I have seen a @inputHidden template helper in the github repository but it is not available in the stable release. How do I accomplish what I am looking for? Thanks.

Tenor answered 16/10, 2012 at 11:35 Comment(2)
Had a similar issue, just posted a solution here: #16911893Cavie
"Handling HTML input creation yourself" #24728877Emirate
I
19

Write it 'manually' as common HTML:

<input type="hidden" name="id" value='@field("id").value' >

or use a way described in the documentation in Handling HTML input creation yourself section.

Interjection answered 16/10, 2012 at 12:16 Comment(4)
I have used this already, what if I have nested form objects? This would work for Customer, how do I do it for Customer.orders?Tenor
I don't know your model or the way how do you manage the relations, so how can I guess?Interjection
Hi, the same worked for nested relations as well. Thanks for your time.Tenor
Just want to add a little note you need a @ sign before field("id"). Final code should look <input type="hidden" name="id" value='@field("id").value' >Fictitious
S
3

Use raw HTML:

<input type="hidden" name="@field("id").name" value='@field("id").value' >
Sinatra answered 26/11, 2014 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.