mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 13:40:55 +00:00
renderform ignore struct field if form tag value is '-'
This commit is contained in:
parent
5c859466ef
commit
459b97858c
22
utils.go
22
utils.go
@ -282,19 +282,23 @@ 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"), ",")
|
||||||
name := fieldT.Name
|
name := fieldT.Name
|
||||||
if len(tags) < 2 {
|
fType := "text"
|
||||||
if len(tags) == 1 && len(tags[0]) > 0 {
|
if len(tags) > 0 && tags[0] == "-" {
|
||||||
name = tags[0]
|
continue
|
||||||
}
|
}
|
||||||
raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="text" value="%v">`,
|
|
||||||
fieldT.Name, name, fieldV.Interface()))
|
if len(tags) == 1 && len(tags[0]) > 0 {
|
||||||
} else {
|
name = tags[0]
|
||||||
|
} else if len(tags) >= 2 {
|
||||||
if len(tags[0]) > 0 {
|
if len(tags[0]) > 0 {
|
||||||
name = tags[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, tags[1], fieldV.Interface()))
|
fType = tags[1]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
raw = append(raw, fmt.Sprintf(`%v: <input name="%v" type="%v" value="%v">`,
|
||||||
|
fieldT.Name, name, fType, fieldV.Interface()))
|
||||||
}
|
}
|
||||||
return template.HTML(strings.Join(raw, "</br>"))
|
return template.HTML(strings.Join(raw, "</br>"))
|
||||||
}
|
}
|
||||||
|
@ -150,10 +150,11 @@ func TestParseForm(t *testing.T) {
|
|||||||
|
|
||||||
func TestRenderForm(t *testing.T) {
|
func TestRenderForm(t *testing.T) {
|
||||||
type user struct {
|
type user struct {
|
||||||
Id int
|
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
|
||||||
Email []string
|
Email []string
|
||||||
Intro string `form:",textarea"`
|
Intro string `form:",textarea"`
|
||||||
}
|
}
|
||||||
@ -165,9 +166,9 @@ func TestRenderForm(t *testing.T) {
|
|||||||
}
|
}
|
||||||
output = RenderForm(&u)
|
output = RenderForm(&u)
|
||||||
result := template.HTML(
|
result := template.HTML(
|
||||||
`Id: <input name="Id" type="text" value="0"></br>` +
|
`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>` +
|
`Age: <input name="age" type="text" value="0"></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 {
|
||||||
t.Errorf("output should equal `%v` but got `%v`", result, output)
|
t.Errorf("output should equal `%v` but got `%v`", result, output)
|
||||||
|
Loading…
Reference in New Issue
Block a user