func (ø *TableForm) Selection(fld string, vals ...interface{}) { field := ø.Field(fld) Selection(field, vals...) label := ø.Label(fld) sel := ø.FieldElement(fld) if sel.Tag() != "select" { innerSelect := SELECT() label.SetContent(innerSelect) field.Element = label field.setFieldInfos() sel = innerSelect } sel.Clear() for _, v := range vals { r := "" typeconverter.Convert(v, &r) sel.Add(OPTION(h.Attr("value", r), h.Text(r))) } }
func TestNewForm(t *testing.T) { //oTyped.Value = iTyped.Value /* my_i := 2 my_f := 3.0 my_i = my_f fmt.Printf("got int: %v\n", my_i) */ f := NewForm( handleErrors, Required( "Id", Int, h.Input(h.Attr("type", "hidden"))), Required( "Name", String, h.Label( h.Text("Your Name: "), h.Input(h.Attr("value", "Enter your Name")))), Required( "intarr", IntArray, h.Input()), Required( "floatarr", FloatArray, h.Input()), Required( "stringarr", StringArray, h.Input()), Required( "Details", Map, h.Input()), Optional( "Address", Constructor(func() interface{} { return &Address{} }), h.Input()), Optional( Person.Name, //Constructor(func() interface{} { return person.New(DB) }), Fill, h.Input()), h.Hr(), h.Div(h.Text("something in between")), Selection( Optional( "height", Float, h.Label(h.Text("Height: ")), h.Select( h.Option(h.Text("one eighty two")), h.Option(h.Text("one")), )), 1.82, 1.0), ) f.Fills[f.Field(Person.Name)] = New(DB) f.BeforeValidation = func(ø *FormHandler) { field := ø.Field("huho") if ø.Strings[field] == "" { ø.Strings[field] = "hiho" } } f.Validation = func(ø *FormHandler) { //fmt.Printf("%#v", ø.Get(person.TABLE.Name).(*person.ROW).ToMap()) height := ø.Field("height") if ø.Floats[height] > 2.0 { ø.AddFieldError(height, fmt.Errorf("%0.2f too large (> 2.0)", ø.Floats[height])) } id := ø.Field("Id") if ø.Floats[height] > float32(ø.Ints[id]) { ø.AddValidationError(fmt.Errorf("height %+v is larger than id %+v", ø.Floats[height], ø.Ints[id])) } } f.Action = func(ø *FormHandler) (err error) { err = ø.Get(Person.Name).(*pgsql.Row).Save() if err != nil { fmt.Println(err) } else { fmt.Println(DB.LastQuery) } /* pers := ø.Get(person.TABLE.Name).(*person.Person) object_id := ø.Get("Id").(int) pers.ObjectId = int(object_id) err = pers.Save() fmt.Println(DB.LastQuery) */ return } vals := map[string]string{ "height": "1.82", "Id": "23", "Name": "Mr Duck", "intarr": "2,4,5", "floatarr": "2.3,4,8.0", "stringarr": "abc, def,ghf", "Details": `{"FirstName": "Donald", "LastName": "Duck", "Age": 144}`, "Person": `{"FirstName": "Donald", "LastName": "Duck", "Age": 144}`, "Address": `{"StreetNo": "Waldweg", "Zip": 12345, "City": "New York"}`, } _ = f.Parse(vals) fl := f.Fields["height"] if f.Floats[fl] != 1.82 { err(t, "incorrect float", f.Floats[fl], 1.82) } st := f.Fields["Name"] if f.Strings[st] != "Mr Duck" { err(t, "incorrect string", f.Strings[st], "Mr Duck") } in := f.Fields["Id"] if f.Ints[in] != 23 { err(t, "incorrect int", f.Ints[in], 23) } ia := f.Fields["intarr"] if f.IntArrays[ia][0] != 2 { err(t, "incorrect intarr[0]", f.IntArrays[ia][0], 2) } if f.IntArrays[ia][1] != 4 { err(t, "incorrect intarr[1]", f.IntArrays[ia][1], 4) } if f.IntArrays[ia][2] != 5 { err(t, "incorrect intarr[2]", f.IntArrays[ia][2], 5) } fa := f.Fields["floatarr"] if f.FloatArrays[fa][0] != 2.3 { err(t, "incorrect floatarr[0]", f.FloatArrays[fa][0], 2.3) } if f.FloatArrays[fa][1] != 4.0 { err(t, "incorrect floatarr[1]", f.FloatArrays[fa][1], 4.0) } if f.FloatArrays[fa][2] != 8.0 { err(t, "incorrect floatarr[2]", f.FloatArrays[fa][2], 8.0) } sa := f.Fields["stringarr"] if f.StringArrays[sa][0] != "abc" { err(t, "incorrect stringarr[0]", f.FloatArrays[sa][0], "abc") } if f.StringArrays[sa][1] != "def" { err(t, "incorrect stringarr[1]", f.FloatArrays[sa][1], "def") } if f.StringArrays[sa][2] != "ghf" { err(t, "incorrect stringarr[2]", f.FloatArrays[sa][2], "ghf") } pers, _ := f.Get("Details").(map[string]interface{}) if pers["FirstName"] != "Donald" { err(t, "incorrect pers[\"FirstName\"]", pers["FirstName"], "Donald") } if pers["LastName"] != "Duck" { err(t, "incorrect pers[\"LastName\"]", pers["LastName"], "Duck") } if pers["Age"] != 144.0 { err(t, "incorrect pers[\"Age\"]", pers["Age"], 144.0) } addr, _ := f.Get("Address").(*Address) if addr.StreetNo != "Waldweg" { err(t, "incorrect addr.StreetNo", addr.StreetNo, "Waldweg") } if addr.Zip != 12345 { err(t, "incorrect addr.Zip", addr.Zip, 12345) } f.Add(h.Id("testform")) fmt.Println(f) }