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
d352e4ab
Unverified
Commit
d352e4ab
authored
Nov 19, 2017
by
astaxie
Committed by
GitHub
Nov 19, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2852 from Radar8/master
validation: fix ErrorMap, added AddError(key, message)
parents
a948d4c1
6641a436
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
4 deletions
+25
-4
validation.go
validation/validation.go
+25
-4
No files found.
validation/validation.go
View file @
d352e4ab
...
...
@@ -112,7 +112,7 @@ type Validation struct {
RequiredFirst
bool
Errors
[]
*
Error
ErrorsMap
map
[
string
]
*
Error
ErrorsMap
map
[
string
]
[]
*
Error
}
// Clear Clean all ValidationError.
...
...
@@ -129,7 +129,7 @@ func (v *Validation) HasErrors() bool {
// ErrorMap Return the errors mapped by key.
// If there are multiple validation errors associated with a single key, the
// first one "wins". (Typically the first validation will be the more basic).
func
(
v
*
Validation
)
ErrorMap
()
map
[
string
]
*
Error
{
func
(
v
*
Validation
)
ErrorMap
()
map
[
string
]
[]
*
Error
{
return
v
.
ErrorsMap
}
...
...
@@ -278,14 +278,35 @@ func (v *Validation) apply(chk Validator, obj interface{}) *Result {
}
}
// Add independent error message for key
func
(
v
*
Validation
)
AddError
(
key
,
message
string
)
{
Name
:=
key
Field
:=
""
parts
:=
strings
.
Split
(
key
,
"."
)
if
len
(
parts
)
==
2
{
Field
=
parts
[
0
]
Name
=
parts
[
1
]
}
err
:=
&
Error
{
Message
:
message
,
Key
:
key
,
Name
:
Name
,
Field
:
Field
,
}
v
.
setError
(
err
)
}
func
(
v
*
Validation
)
setError
(
err
*
Error
)
{
v
.
Errors
=
append
(
v
.
Errors
,
err
)
if
v
.
ErrorsMap
==
nil
{
v
.
ErrorsMap
=
make
(
map
[
string
]
*
Error
)
v
.
ErrorsMap
=
make
(
map
[
string
]
[]
*
Error
)
}
if
_
,
ok
:=
v
.
ErrorsMap
[
err
.
Field
];
!
ok
{
v
.
ErrorsMap
[
err
.
Field
]
=
err
v
.
ErrorsMap
[
err
.
Field
]
=
[]
*
Error
{}
}
v
.
ErrorsMap
[
err
.
Field
]
=
append
(
v
.
ErrorsMap
[
err
.
Field
],
err
)
}
// SetError Set error message for one field in ValidationError
...
...
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