4

I have a play template looking like this:

@(projects: List[Project], projectForm: Form[Project])

@import helper._
@main("Create projects") {
    <div class="accordion">
    @for(project <- projects) {
        <h3>@project.name</h3>
        <div>
        @form(routes.Application.updateProject(project.getId), 'class -> "ajaxForm") {
            @* I'm not even sure why I need to specify the FQN of Map here *@
            @defining(projectForm.bind(scala.collection.mutable.Map( "name" -> project.name,
                "description" -> project.description))) { form =>
                @inputText(form("name"))
                @textarea(form("description"))
                <input type="submit" value="Update"/>
            }
        }
        </div>

    }
    </div>
    @form(routes.Application.createProject()) {
        <fieldset>
            <legend>Create a new project</legend>
            @inputText(projectForm("name"))
            @textarea(projectForm("description"))
            <input value="create" type="submit"/>
        </fieldset>
    }
}

Project is a model containing a long id and a String name and description.

My problem here is that here

@inputText(form("name"), 'value -> project.name)
@textarea(form("description"))

The inputText and textarea always get the ids name and description respectively. I have many of those though, so these ids aren't unique anymore. This still works on chrome, but I understand that ids have to be unique in a document. Is there some built-in way in Play to deal with this sort of problem, or will I have to come up with an own solution? If its the latter, do you have some suggestions on how to approach this? Or am I doing something fundamentally wrong?

Cubic
  • 14,902
  • 5
  • 47
  • 92
  • Regarding your comment about why you have to use FQN for map is because it is mutable. The immutable variants are imported by default. So the question is why you would be using the mutable Map here? – maba May 30 '13 at 10:11
  • @maba With the unqualified Map I get compilation errors ("object java.util.Map is not a value"). Apparently it thinks that I mean `java.util.Map`, which is weird considering I'm in a scala source and didn't even import it. Apparently the immutable map works too, but I still have to qualify it. – Cubic May 30 '13 at 10:13

1 Answers1

4

You can use the list index of the project and append that to the id:

@for((project, index) <- projects.zipWithIndex) {
    <h3>@project.name</h3>
    <div>
    @form(routes.Application.updateProject(project.getId), 'class -> "ajaxForm") {
        @defining(projectForm.bind(scala.collection.mutable.Map(
            "name" -> project.name,
            "description" -> project.description))) { form =>
            @inputText(form("name"), 'id -> ("name" + index))
            @textarea(form("description"), 'id -> ("description" + index))
            <input type="submit" value="Update"/>
        }
    }
    </div>
}
maba
  • 47,113
  • 10
  • 108
  • 118
  • 2
    This works. But do you mind explaining _how_? I mean, now that there are numbers in the ids, how does form.bindFromRequest get the right values? – Cubic May 30 '13 at 10:34
  • I doesn't matter. Even though there are several forms on the page there's only one form where the submit button is pressed and it will post those values to your controller. It is the `name` attribute that is used to identify the form parameter: [Difference between id and name in HTML](http://stackoverflow.com/questions/1397592/difference-between-id-and-name-attributes-in-html) – maba May 30 '13 at 10:44