mirror of
https://github.com/astaxie/beego.git
synced 2024-11-23 04:20:54 +00:00
Merge pull request #150 from miraclesu/form
Support custom label for renderform
This commit is contained in:
commit
328f4566e4
27
utils.go
27
utils.go
@ -281,24 +281,39 @@ func RenderForm(obj interface{}) template.HTML {
|
|||||||
|
|
||||||
fieldT := objT.Field(i)
|
fieldT := objT.Field(i)
|
||||||
tags := strings.Split(fieldT.Tag.Get("form"), ",")
|
tags := strings.Split(fieldT.Tag.Get("form"), ",")
|
||||||
|
label := fieldT.Name + ": "
|
||||||
name := fieldT.Name
|
name := fieldT.Name
|
||||||
fType := "text"
|
fType := "text"
|
||||||
if len(tags) > 0 && tags[0] == "-" {
|
|
||||||
|
switch len(tags) {
|
||||||
|
case 1:
|
||||||
|
if tags[0] == "-" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if len(tags[0]) > 0 {
|
||||||
if len(tags) == 1 && len(tags[0]) > 0 {
|
|
||||||
name = tags[0]
|
name = tags[0]
|
||||||
} else if len(tags) >= 2 {
|
}
|
||||||
|
case 2:
|
||||||
if len(tags[0]) > 0 {
|
if len(tags[0]) > 0 {
|
||||||
name = tags[0]
|
name = tags[0]
|
||||||
}
|
}
|
||||||
if len(tags[1]) > 0 {
|
if len(tags[1]) > 0 {
|
||||||
fType = tags[1]
|
fType = tags[1]
|
||||||
}
|
}
|
||||||
|
case 3:
|
||||||
|
if len(tags[0]) > 0 {
|
||||||
|
name = tags[0]
|
||||||
}
|
}
|
||||||
raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="%v" value="%v">`,
|
if len(tags[1]) > 0 {
|
||||||
fieldT.Name, name, fType, fieldV.Interface()))
|
fType = tags[1]
|
||||||
|
}
|
||||||
|
if len(tags[2]) > 0 {
|
||||||
|
label = tags[2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
raw = append(raw, fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`,
|
||||||
|
label, name, fType, fieldV.Interface()))
|
||||||
}
|
}
|
||||||
return template.HTML(strings.Join(raw, "</br>"))
|
return template.HTML(strings.Join(raw, "</br>"))
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@ func TestRenderForm(t *testing.T) {
|
|||||||
Id int `form:"-"`
|
Id int `form:"-"`
|
||||||
tag string `form:"tag"`
|
tag string `form:"tag"`
|
||||||
Name interface{} `form:"username"`
|
Name interface{} `form:"username"`
|
||||||
Age int `form:"age,text"`
|
Age int `form:"age,text,年龄:"`
|
||||||
Sex string
|
Sex string
|
||||||
Email []string
|
Email []string
|
||||||
Intro string `form:",textarea"`
|
Intro string `form:",textarea"`
|
||||||
@ -167,7 +167,7 @@ func TestRenderForm(t *testing.T) {
|
|||||||
output = RenderForm(&u)
|
output = RenderForm(&u)
|
||||||
result := template.HTML(
|
result := template.HTML(
|
||||||
`Name: <input name="username" type="text" value="test"></br>` +
|
`Name: <input name="username" type="text" value="test"></br>` +
|
||||||
`Age: <input name="age" type="text" value="0"></br>` +
|
`年龄:<input name="age" type="text" value="0"></br>` +
|
||||||
`Sex: <input name="Sex" type="text" value=""></br>` +
|
`Sex: <input name="Sex" type="text" value=""></br>` +
|
||||||
`Intro: <input name="Intro" type="textarea" value="">`)
|
`Intro: <input name="Intro" type="textarea" value="">`)
|
||||||
if output != result {
|
if output != result {
|
||||||
|
Loading…
Reference in New Issue
Block a user