Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
459b9785
Commit
459b9785
authored
Aug 11, 2013
by
miraclesu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renderform ignore struct field if form tag value is '-'
parent
5c859466
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
13 deletions
+18
-13
utils.go
utils.go
+13
-9
utils_test.go
utils_test.go
+5
-4
No files found.
utils.go
View file @
459b9785
...
...
@@ -282,19 +282,23 @@ func RenderForm(obj interface{}) template.HTML {
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
{
fType
:=
"text"
if
len
(
tags
)
>
0
&&
tags
[
0
]
==
"-"
{
continue
}
if
len
(
tags
)
==
1
&&
len
(
tags
[
0
])
>
0
{
name
=
tags
[
0
]
}
else
if
len
(
tags
)
>=
2
{
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
()))
if
len
(
tags
[
1
])
>
0
{
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>"
))
}
...
...
utils_test.go
View file @
459b9785
...
...
@@ -150,10 +150,11 @@ func TestParseForm(t *testing.T) {
func
TestRenderForm
(
t
*
testing
.
T
)
{
type
user
struct
{
Id
int
tag
string
`form:
tag
`
Id
int
`form:"-"`
tag
string
`form:
"tag"
`
Name
interface
{}
`form:"username"`
Age
int
`form:"age,text"`
Sex
string
Email
[]
string
Intro
string
`form:",textarea"`
}
...
...
@@ -165,9 +166,9 @@ func TestRenderForm(t *testing.T) {
}
output
=
RenderForm
(
&
u
)
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>`
+
`Sex: <input name="Sex" type="text" value=""></br>`
+
`Intro: <input name="Intro" type="textarea" value="">`
)
if
output
!=
result
{
t
.
Errorf
(
"output should equal `%v` but got `%v`"
,
result
,
output
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment