Zip style @repeat over nested form

@repeat extremely useful; however, I hit the road block with nested forms.

I need to create a form for a game schedule that has 2 attributes, schedule information (game date, time, place, opponent) and sending team notes (for example, "due to a winter storm, the game on January 7 was rescheduled for January 9 at .. . Hawaii ;-) ")

Form mapping is based on:

 case class Schedule( composite: Seq[Composite], note: Seq[ScheduleNote] ) 

and then display the form in the template that I have:

 @repeat(_form("composite"), min=numGames) { f=> @inputDate(f("gameDate"), 'id-> "gameDate", '_label-> "Game Date") .... } @repeat(_form("note"), min=numGames) { f=> @inputDate(f("gameNote"), '_label-> "Game Notes") .... } 

Of course, game notes must be paired with game data, which will not be in the above, since it seems to me that I need @repeat composite game data and notes separately.

It would be really, really nice: @repeat(_form("composite").zip(_form("note")), min=numGames) { case(fc,fn)=>

over nested form elements.

Anyway, can I take it off? If you look at the source , this is not so, but it is possible with my library's pimp (or, since I'm building against 2.1, hack something in until the environment supports what seems to be a limitation)

+4
source share
1 answer

EDIT
In fact, my initial attempt doubled the number of fields produced; this generates the correct number of fields:

 object repeat2 { import play.api.data.Field, play.api.templates.Html def apply(field: (Field,Field), min: Int = 1)(f: (Field,Field) => Html) = { field match{ case(a,b)=> (0 until math.max( if (a.indexes.isEmpty) 0 else a.indexes.max + 1, min) ).map(i => f.apply(a("["+i+"]"), b("["+i+"]")) ) } } } 

Still TBD if the edit form maps correctly form data values โ€‹โ€‹....

ORIGINAL
Experimenting this compiles:

 // in a form utility object object repeat2 { import play.api.data.Field, play.api.templates.Html def apply(field: (Field,Field), min: Int = 1)(f: Field => Html) = { field match{ case(a,b)=> (0 until math.max( if (a.indexes.isEmpty) 0 else a.indexes.max + 1, min) ).map(i => f(a("["+i+"]")) + f(b("["+i+"]")) ) } } } // then, importing above in a template @repeat2( (_form("composite"), _form("note")), min=5) { f=> @inputDate(f("gameDate"), 'id-> "gameDate", '_label-> "Game Date") ... @inputDate(f("gameNote"), '_label-> "Game Notes") } 

and if necessary generates game data and notes together.

As for whether he is working on form editing, TBD; -)

+4
source

Source: https://habr.com/ru/post/1439653/


All Articles