Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto render a form with list mapping

I have a Form with a nested list of mappings, but have problems iterating this in the template. The Form looks like this

val assignmentForm : Form[AssignmentData] = Form(
   "acceptedSign" -> nonEmptyText(),
    mapping("activities" -> list (    
      mapping("activityId" -> optional(text), 
          "activityStatus" -> optional(text)) 
          (ActivityData.apply)(ActivityData.unapply))
    )(AssignmentData.apply)(AssignmentData.unapply)
)

I am passing this form into the template and try to iterate over the activities. So far only compiler errors or a complete metadata dump of the Form as a result.

This gives a metadata+data dump of the complete form, with the activity included. (the LI tag is a tab navigator that will contain input fields)

@repeat(assignmentForm("activities")) { activity =>
                    <li>@activity("activityId")</li>
                    }

Iteration sort of works (is the code runs), but it is completely unusable from a user standpoint.

I have also tried various for-loops, only giving me compiler errors, saying

value map is not a member of play.api.data.Field

My questions are:

  1. Is it supposed to be possible to construct such a form, and how is it done?
  2. What other options do I have for rendering input forms with one-to-many relationsships?
like image 695
dagb Avatar asked Oct 14 '12 19:10

dagb


1 Answers

The solution is as simple as this, navtab.scala.html

@**
* Generate an LI element
*@

@(field: play.api.data.Field)
<li><a href='#@field.id' data-toggle="modal">@field("activityStatus").value</a></li>

Then repeat for the mapped collection in your template

<ul  id="activities">
    @repeat(assignmentForm("activities"), min=0) { activity =>
        @navtab(activity)
    }
</ul>
like image 139
dagb Avatar answered Nov 15 '22 04:11

dagb