@@ -2,6 +2,7 @@ package form_test
2
2
3
3
import (
4
4
"database/sql"
5
+ "strconv"
5
6
"testing"
6
7
"time"
7
8
@@ -159,8 +160,10 @@ func Test_FormFor_NullableField(t *testing.T) {
159
160
}
160
161
161
162
type Person struct {
162
- Name string
163
- Address Address
163
+ Name string
164
+ Address Address
165
+ References []Address
166
+ Contacts []string
164
167
}
165
168
166
169
type Address struct {
@@ -185,6 +188,118 @@ func Test_FormFor_Nested_Struct(t *testing.T) {
185
188
r .Equal (exp , tag .String ())
186
189
}
187
190
191
+ func Test_FormFor_Nested_Slice_Struct (t * testing.T ) {
192
+ r := require .New (t )
193
+ p := Person {
194
+ Name : "Mark" ,
195
+ Address : Address {
196
+ City : "Boston" ,
197
+ State : "MA" ,
198
+ },
199
+ }
200
+ p .References = []Address {p .Address }
201
+
202
+ f := form .NewFormFor (p , tags.Options {})
203
+ tag := f .InputTag ("References[0].City" , tags.Options {})
204
+
205
+ exp := `<input id="person-References[0].City" name="References[0].City" type="text" value="Boston" />`
206
+ r .Equal (exp , tag .String ())
207
+ }
208
+
209
+ func Test_FormFor_Nested_Slice_String (t * testing.T ) {
210
+ r := require .New (t )
211
+ p := Person {
212
+ Contacts : []string {
213
+ "Contact 1" ,
214
+ "Contact 2" ,
215
+ "Contact 3" ,
216
+ },
217
+ }
218
+
219
+ f := form .NewFormFor (p , tags.Options {})
220
+
221
+ for i := 0 ; i < len (p .Contacts ); i ++ {
222
+ expectedValue := p .Contacts [i ]
223
+ index := strconv .Itoa (i )
224
+ tag := f .InputTag ("Contacts[" + index + "]" , tags.Options {})
225
+ exp := `<input id="person-Contacts[` + index + `]" name="Contacts[` + index + `]" type="text" value="` + expectedValue + `" />`
226
+ r .Equal (exp , tag .String ())
227
+ }
228
+ }
229
+
230
+ func Test_FormFor_Nested_Slice_String_Pointer (t * testing.T ) {
231
+ r := require .New (t )
232
+ p := struct {
233
+ Contacts * []string
234
+ }{
235
+ & []string {"Contact 1" , "Contact 2" },
236
+ }
237
+
238
+ f := form .NewFormFor (p , tags.Options {})
239
+ tag := f .InputTag ("Contacts[0]" , tags.Options {})
240
+
241
+ exp := `<input id="-Contacts[0]" name="Contacts[0]" type="text" value="Contact 1" />`
242
+ r .Equal (exp , tag .String ())
243
+ }
244
+
245
+ func Test_FormFor_Nested_Slice_Pointer (t * testing.T ) {
246
+ r := require .New (t )
247
+ p := struct {
248
+ Persons * []Person
249
+ }{
250
+ & []Person {{Name : "Mark" }, {Name : "Clayton" }, {Name : "Iain" }},
251
+ }
252
+
253
+ f := form .NewFormFor (p , tags.Options {})
254
+
255
+ for i := 0 ; i < len (* p .Persons ); i ++ {
256
+ expectedValue := (* p .Persons )[i ].Name
257
+ index := strconv .Itoa (i )
258
+ tag := f .InputTag ("Persons[" + index + "].Name" , tags.Options {})
259
+ exp := `<input id="-Persons[` + index + `].Name" name="Persons[` + index + `].Name" type="text" value="` + expectedValue + `" />`
260
+ r .Equal (exp , tag .String ())
261
+ }
262
+ }
263
+
264
+ func Test_FormFor_Nested_Slice_Pointer_Elements (t * testing.T ) {
265
+ r := require .New (t )
266
+ p := struct {
267
+ Persons []* Person
268
+ }{
269
+ []* Person {
270
+ & Person {Name : "Mark" },
271
+ },
272
+ }
273
+
274
+ f := form .NewFormFor (p , tags.Options {})
275
+ tag := f .InputTag ("Persons[0].Name" , tags.Options {})
276
+
277
+ exp := `<input id="-Persons[0].Name" name="Persons[0].Name" type="text" value="Mark" />`
278
+ r .Equal (exp , tag .String ())
279
+ }
280
+
281
+ func Test_FormFor_Nested_Slice_With_Sub_Slices (t * testing.T ) {
282
+ r := require .New (t )
283
+ p := struct {
284
+ Persons * []Person
285
+ }{
286
+ & []Person {
287
+ {
288
+ Name : "Mark" ,
289
+ References : []Address {
290
+ {City : "Boston" },
291
+ },
292
+ },
293
+ },
294
+ }
295
+
296
+ f := form .NewFormFor (p , tags.Options {})
297
+ tag := f .InputTag ("Persons[0].References[0].City" , tags.Options {})
298
+
299
+ exp := `<input id="-Persons[0].References[0].City" name="Persons[0].References[0].City" type="text" value="Boston" />`
300
+ r .Equal (exp , tag .String ())
301
+ }
302
+
188
303
func Test_FormFor_DateTimeTag (t * testing.T ) {
189
304
r := require .New (t )
190
305
0 commit comments