multiple instances of a Form

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?


您可以使用项目的列表索引并将其附加到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>
}
链接地址: http://www.djcxy.com/p/88050.html

上一篇: 使用javascript获取输入的值

下一篇: 表单的多个实例