mirror of
https://github.com/astaxie/beego.git
synced 2024-11-06 07:20:56 +00:00
fix label == `` #4001
This commit is contained in:
parent
4ffe26a1d2
commit
70733d9810
@ -15,6 +15,7 @@
|
|||||||
package validation
|
package validation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -80,6 +81,28 @@ func TestGetValidFuncs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Name string `valid:"Required;MaxSize(5)" `
|
||||||
|
Sex string `valid:"Required;" label:"sex_label"`
|
||||||
|
Age int `valid:"Required;Range(1, 140);" label:"age_label"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidation(t *testing.T) {
|
||||||
|
u := User{"man1238888456", "", 1140}
|
||||||
|
valid := Validation{}
|
||||||
|
b, err := valid.Valid(&u)
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
if !b {
|
||||||
|
// validation does not pass
|
||||||
|
// blabla...
|
||||||
|
for _, err := range valid.Errors {
|
||||||
|
log.Println(err.Key, err.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCall(t *testing.T) {
|
func TestCall(t *testing.T) {
|
||||||
u := user{Name: "test", Age: 180}
|
u := user{Name: "test", Age: 180}
|
||||||
tf := reflect.TypeOf(u)
|
tf := reflect.TypeOf(u)
|
||||||
|
@ -273,10 +273,13 @@ func (v *Validation) apply(chk Validator, obj interface{}) *Result {
|
|||||||
Field = parts[0]
|
Field = parts[0]
|
||||||
Name = parts[1]
|
Name = parts[1]
|
||||||
Label = parts[2]
|
Label = parts[2]
|
||||||
|
if len(Label) == 0 {
|
||||||
|
Label = Field
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := &Error{
|
err := &Error{
|
||||||
Message: Label + chk.DefaultMessage(),
|
Message: Label + " " + chk.DefaultMessage(),
|
||||||
Key: key,
|
Key: key,
|
||||||
Name: Name,
|
Name: Name,
|
||||||
Field: Field,
|
Field: Field,
|
||||||
@ -293,19 +296,25 @@ func (v *Validation) apply(chk Validator, obj interface{}) *Result {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// key must like aa.bb.cc or aa.bb.
|
||||||
// AddError adds independent error message for the provided key
|
// AddError adds independent error message for the provided key
|
||||||
func (v *Validation) AddError(key, message string) {
|
func (v *Validation) AddError(key, message string) {
|
||||||
Name := key
|
Name := key
|
||||||
Field := ""
|
Field := ""
|
||||||
|
|
||||||
|
Label := ""
|
||||||
parts := strings.Split(key, ".")
|
parts := strings.Split(key, ".")
|
||||||
if len(parts) == 3 {
|
if len(parts) == 3 {
|
||||||
Field = parts[0]
|
Field = parts[0]
|
||||||
Name = parts[1]
|
Name = parts[1]
|
||||||
|
Label = parts[2]
|
||||||
|
if len(Label) == 0 {
|
||||||
|
Label = Field
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := &Error{
|
err := &Error{
|
||||||
Message: message,
|
Message: Label + " " + message,
|
||||||
Key: key,
|
Key: key,
|
||||||
Name: Name,
|
Name: Name,
|
||||||
Field: Field,
|
Field: Field,
|
||||||
@ -381,7 +390,6 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
chk := Required{""}.IsSatisfied(currentField)
|
chk := Required{""}.IsSatisfied(currentField)
|
||||||
if !hasRequired && v.RequiredFirst && !chk {
|
if !hasRequired && v.RequiredFirst && !chk {
|
||||||
if _, ok := CanSkipFuncs[vf.Name]; ok {
|
if _, ok := CanSkipFuncs[vf.Name]; ok {
|
||||||
|
Loading…
Reference in New Issue
Block a user