mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 14:40:57 +00:00
Merge pull request #143 from miraclesu/form
Add renderform template function
This commit is contained in:
commit
bc060c95f8
@ -31,6 +31,7 @@ func init() {
|
||||
beegoTplFuncMap["str2html"] = Str2html
|
||||
beegoTplFuncMap["htmlquote"] = Htmlquote
|
||||
beegoTplFuncMap["htmlunquote"] = Htmlunquote
|
||||
beegoTplFuncMap["renderform"] = RenderForm
|
||||
}
|
||||
|
||||
// AddFuncMap let user to register a func in the template
|
||||
|
74
utils.go
74
utils.go
@ -178,7 +178,7 @@ func inSlice(v string, sl []string) bool {
|
||||
func ParseForm(form url.Values, obj interface{}) error {
|
||||
objT := reflect.TypeOf(obj)
|
||||
objV := reflect.ValueOf(obj)
|
||||
if !(objT.Kind() == reflect.Ptr && objT.Elem().Kind() == reflect.Struct) {
|
||||
if !isStructPtr(objT) {
|
||||
return fmt.Errorf("%v must be a struct pointer", obj)
|
||||
}
|
||||
objT = objT.Elem()
|
||||
@ -189,11 +189,16 @@ func ParseForm(form url.Values, obj interface{}) error {
|
||||
if !fieldV.CanSet() {
|
||||
continue
|
||||
}
|
||||
|
||||
fieldT := objT.Field(i)
|
||||
tag := fieldT.Tag.Get("form")
|
||||
if len(tag) == 0 {
|
||||
tags := strings.Split(fieldT.Tag.Get("form"), ",")
|
||||
var tag string
|
||||
if len(tags) == 0 || len(tags[0]) == 0 {
|
||||
tag = fieldT.Name
|
||||
} else {
|
||||
tag = tags[0]
|
||||
}
|
||||
|
||||
value := form.Get(tag)
|
||||
if len(value) == 0 {
|
||||
continue
|
||||
@ -233,6 +238,69 @@ func ParseForm(form url.Values, obj interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// form types for RenderForm function
|
||||
var FormType = map[string]bool{
|
||||
"text": true,
|
||||
"textarea": true,
|
||||
"hidden": true,
|
||||
"password": true,
|
||||
}
|
||||
|
||||
var unKind = map[reflect.Kind]bool{
|
||||
reflect.Uintptr: true,
|
||||
reflect.Complex64: true,
|
||||
reflect.Complex128: true,
|
||||
reflect.Array: true,
|
||||
reflect.Chan: true,
|
||||
reflect.Func: true,
|
||||
reflect.Map: true,
|
||||
reflect.Ptr: true,
|
||||
reflect.Slice: true,
|
||||
reflect.Struct: true,
|
||||
reflect.UnsafePointer: true,
|
||||
}
|
||||
|
||||
// obj must be a struct pointer
|
||||
func RenderForm(obj interface{}) template.HTML {
|
||||
objT := reflect.TypeOf(obj)
|
||||
objV := reflect.ValueOf(obj)
|
||||
if !isStructPtr(objT) {
|
||||
return template.HTML("")
|
||||
}
|
||||
objT = objT.Elem()
|
||||
objV = objV.Elem()
|
||||
|
||||
var raw []string
|
||||
for i := 0; i < objT.NumField(); i++ {
|
||||
fieldV := objV.Field(i)
|
||||
if !fieldV.CanSet() || unKind[fieldV.Kind()] {
|
||||
continue
|
||||
}
|
||||
|
||||
fieldT := objT.Field(i)
|
||||
tags := strings.Split(fieldT.Tag.Get("form"), ",")
|
||||
name := fieldT.Name
|
||||
if len(tags) < 2 {
|
||||
if len(tags) == 1 && len(tags[0]) > 0 {
|
||||
name = tags[0]
|
||||
}
|
||||
raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="text" value="%v">`,
|
||||
fieldT.Name, name, fieldV.Interface()))
|
||||
} else {
|
||||
if len(tags[0]) > 0 {
|
||||
name = tags[0]
|
||||
}
|
||||
raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="%v" value="%v">`,
|
||||
fieldT.Name, name, tags[1], fieldV.Interface()))
|
||||
}
|
||||
}
|
||||
return template.HTML(strings.Join(raw, "</br>"))
|
||||
}
|
||||
|
||||
func isStructPtr(t reflect.Type) bool {
|
||||
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
|
||||
}
|
||||
|
||||
func stringsToJson(str string) string {
|
||||
rs := []rune(str)
|
||||
jsons := ""
|
||||
|
@ -1,6 +1,7 @@
|
||||
package beego
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
@ -106,8 +107,9 @@ func TestParseForm(t *testing.T) {
|
||||
Id int
|
||||
tag string `form:tag`
|
||||
Name interface{} `form:"username"`
|
||||
Age int `form:"age"`
|
||||
Age int `form:"age,text"`
|
||||
Email string
|
||||
Intro string `form:",textarea"`
|
||||
}
|
||||
|
||||
u := user{}
|
||||
@ -116,6 +118,7 @@ func TestParseForm(t *testing.T) {
|
||||
"username": []string{"test"},
|
||||
"age": []string{"40"},
|
||||
"Email": []string{"test@gmail.com"},
|
||||
"Intro": []string{"I am an engineer!"},
|
||||
}
|
||||
if err := ParseForm(form, u); err == nil {
|
||||
t.Fatal("nothing will be changed")
|
||||
@ -124,18 +127,47 @@ func TestParseForm(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u.Id != 0 {
|
||||
t.Error("Id should not be changed")
|
||||
t.Errorf("Id should equal 0 but got %v", u.Id)
|
||||
}
|
||||
if len(u.tag) != 0 {
|
||||
t.Error("tag should not be changed")
|
||||
t.Errorf("tag's length should equal 0 but got %v", len(u.tag))
|
||||
}
|
||||
if u.Name.(string) != "test" {
|
||||
t.Error("should be equal")
|
||||
t.Errorf("Name should equal `test` but got `%v`", u.Name.(string))
|
||||
}
|
||||
if u.Age != 40 {
|
||||
t.Error("should be equal")
|
||||
t.Errorf("Age should equal 40 but got %v", u.Age)
|
||||
}
|
||||
if u.Email != "test@gmail.com" {
|
||||
t.Error("should be equal")
|
||||
t.Errorf("Email should equal `test@gmail.com` but got `%v`", u.Email)
|
||||
}
|
||||
if u.Intro != "I am an engineer!" {
|
||||
t.Errorf("Intro should equal `I am an engineer!` but got `%v`", u.Intro)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderForm(t *testing.T) {
|
||||
type user struct {
|
||||
Id int
|
||||
tag string `form:tag`
|
||||
Name interface{} `form:"username"`
|
||||
Age int `form:"age,text"`
|
||||
Email []string
|
||||
Intro string `form:",textarea"`
|
||||
}
|
||||
|
||||
u := user{Name: "test"}
|
||||
output := RenderForm(u)
|
||||
if output != template.HTML("") {
|
||||
t.Errorf("output should be empty but got %v", output)
|
||||
}
|
||||
output = RenderForm(&u)
|
||||
result := template.HTML(
|
||||
`Id: <input name="Id" type="text" value="0"></br>` +
|
||||
`Name: <input name="username" type="text" value="test"></br>` +
|
||||
`Age: <input name="age" type="text" value="0"></br>` +
|
||||
`Intro: <input name="Intro" type="textarea" value="">`)
|
||||
if output != result {
|
||||
t.Errorf("output should equal `%v` but got `%v`", result, output)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user