Exemple #1
2
func Selection(Field *Field, vals ...interface{}) *Field {
	switch Field.Type {
	case Int:
		allowed := []int{}
		for _, v := range vals {
			var i int
			typeconverter.Convert(v, &i)
			allowed = append(allowed, i)
		}
		Field.Selection = allowed
	case Float:
		allowed := []float32{}
		for _, v := range vals {
			iv, ok := v.(float32)
			if !ok {
				iv = float32(v.(float64))
			}

			allowed = append(allowed, iv)
		}
		Field.Selection = allowed
	case String:
		allowed := []string{}
		for _, v := range vals {
			allowed = append(allowed, v.(string))
		}
		Field.Selection = allowed
	}
	if sel := Field.Element.Any(h.Tag("select")); sel != nil {
		options := sel.All(h.Tag("option"))
		for i, opt := range options {
			var str string
			typeconverter.Convert(vals[i], &str)
			opt.Add(h.Attr("value", str))
		}
	}
	return Field
}
Exemple #2
1
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)))
	}
}
Exemple #3
0
func (ø *TableForm) SetValues(row *pgsql.Row) {
	props := row.AsStrings()
	for k, v := range props {
		if !ø.HasFieldDefinition(k) {
			continue
		}
		elem := ø.FieldElement(k)
		if elem.Tag() == "select" {
			option := elem.Any(h.And_(h.Attr("value", v), h.Tag("option")))
			if option != nil {
				option.Add(h.Attr("selected", "selected"))
			}
		} else {
			if elem.Tag() == "textarea" {
				elem.Add(v)
			} else {
				//tp := elem.Attribute("type")
				//if tp == "date" {
				if elem.HasClass("date") {
					var tme time.Time
					field := row.Table.Field(k)
					row.Get(field, &tme)
					year, month, day := tme.Date()
					// %02.0f.%02.0f.%4.0f
					v = fmt.Sprintf("%4.0f-%02.0f-%02.0f", float64(year), float64(int(month)), float64(day))
				}
				elem.Add(h.Attr("value", v))
			}
		}
	}
}
Exemple #4
0
// sets the infos of the inner Field tag
func (ø *Field) setFieldInfos() {
	fs := ø.Element.Fields()
	if len(fs) == 0 {
		panic("got no form Field in " + ø.Element.String())
	}
	fs[0].Add(h.Class("field"), h.Id(ø.Name), h.Attr("name", ø.Name))
	if ø.Required {
		fs[0].Add(h.Attr("required", "required"))
		fs[0].AddClass(h.Class("required"))
	}
	ø.setLabelInfos()
}
Exemple #5
0
// sets all true and false values to the given text
func (ø *TableForm) SetBoolTexts(trueText string, falseText string) {
	for n, f := range ø.Fields {
		if f.Type == Bool {
			elem := ø.FieldElement(n)
			trueOpt := elem.Any(h.And_(h.Attr("value", "true"), h.Tag("option")))
			if trueOpt != nil {
				trueOpt.SetContent(trueText)
			}
			falseOpt := elem.Any(h.And_(h.Attr("value", "false"), h.Tag("option")))
			if falseOpt != nil {
				falseOpt.SetContent(falseText)
			}
		}
	}
}
Exemple #6
0
func (ø *Field) setLabelInfos() {
	label := ø.Element.Any(h.Tag("label"))
	if label != nil {
		label.Add(h.Attr("for", ø.Name))
		if ø.Required {
			label.Add(h.Class("required"))
		}
	}
}
Exemple #7
0
func (ø *FormHandler) AddSubmitButton(value string) (el *h.Element) {
	el = INPUT(
		h.Class("submit"),
		h.Class("btn"),
		h.Class("btn-primary"),
		h.Attr("type", "submit", "value", value))
	ø.Element.Add(el)
	return
}
Exemple #8
0
func (ø *TableForm) Require(fld string) {
	e := ø.Label(fld)
	e.AddClass("required")
	ø.FieldElement(fld).Add(h.Attr("required", "required"))
	field := ø.Field(fld)
	if !field.Required {
		ø.AddFieldToRequired(field)
		field.Required = true
	}
}
Exemple #9
0
func GetElement(in *pgsql.Field) (out *h.Element) {
	if in.Selection != nil {
		return SELECT()
	}
	if pgsql.IsVarChar(in.Type) {
		return INPUT(h.Attr("type", "text"))
	}

	if in.Type == pgsql.BoolType {
		return SELECT(
			OPTION(h.Attr("value", "true"), "true"),
			OPTION(h.Attr("value", "false"), "false"),
		)
	}

	switch in.Type {
	case pgsql.TextType:
		return TEXTAREA()
	case pgsql.XmlType:
		return TEXTAREA(h.Class("xml"))
	case pgsql.HtmlType:
		return TEXTAREA(h.Class("html"))
	case pgsql.IntType:
		return INPUT(h.Attr("type", "number"))
	case pgsql.UuidType:
		if in.ForeignKey != nil {
			return INPUT(h.Attr("type", "text", "fkey", in.ForeignKey.Table.Name), h.Class("foreign-key"))
		}
		return INPUT(h.Attr("type", "text"))
	case pgsql.DateType:
		return INPUT(h.Attr("type", "text"), h.Class("date"))
	case pgsql.TimeType:
		return INPUT(h.Attr("type", "time"))
	}
	return INPUT(h.Attr("type", "text"))
}
Exemple #10
0
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)

}
Exemple #11
0
func (ø *TableForm) Label(fld string) (e *h.Element) {
	e = ø.Any(h.And_(h.Tag("label"), h.Attr("for", fld)))
	return
}
Exemple #12
0
func (ø *TableForm) getElement(in *pgsql.Field) (out *h.Element) {
	if in.Selection != nil {
		ø.afterCreation = append(ø.afterCreation, func(tf *TableForm) {
			sell := []interface{}{}
			for _, ss := range in.Selection {
				sell = append(sell, ss)
			}
			tf.Selection(in.Name, sell...)
		})
		return SELECT()
	}
	if pgsql.IsVarChar(in.Type) {
		return INPUT(h.Attr("type", "text"))
	}

	if in.Type == pgsql.BoolType {
		return SELECT(
			OPTION(h.Attr("value", "true"), "true"),
			OPTION(h.Attr("value", "false"), "false"),
		)
	}

	switch in.Type {
	case pgsql.TextType:
		return TEXTAREA()
	case pgsql.XmlType:
		return TEXTAREA(h.Class("xml"))
	case pgsql.HtmlType:
		return TEXTAREA(h.Class("html"))
	case pgsql.UuidType:
		if in.ForeignKey != nil {
			return INPUT(h.Attr("type", "text", "fkey", in.ForeignKey.Table.Name), h.Class("foreign-key"))
		}
		return INPUT(h.Attr("type", "text"))
	case pgsql.IntType:
		return INPUT(h.Attr("type", "number"))
	case pgsql.DateType:
		return INPUT(h.Class("date"), h.Attr("type", "text"))
	case pgsql.TimeType:
		return INPUT(h.Attr("type", "time"))
	}
	return INPUT(h.Attr("type", "text"))

	/*
	 input[type="password"]:focus,
	 input[type="datetime"]:focus,
	 input[type="datetime-local"]:focus,
	 input[type="date"]:focus,
	 input[type="month"]:focus,
	 input[type="time"]:focus,
	 input[type="week"]:focus,
	 input[type="number"]:focus,
	 input[type="email"]:focus,
	 input[type="url"]:focus,
	 input[type="search"]:focus,
	 input[type="tel"]:focus,
	 input[type="color"]:focus,
	 .uneditable-input:focus
	*/

}