예제 #1
0
파일: auth.go 프로젝트: nathan7/gogs
func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaron.Locale) binding.Errors {
	if errs.Len() == 0 {
		return errs
	}

	data["HasError"] = true
	AssignForm(f, data)

	typ := reflect.TypeOf(f)
	val := reflect.ValueOf(f)

	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
		val = val.Elem()
	}

	for i := 0; i < typ.NumField(); i++ {
		field := typ.Field(i)

		fieldName := field.Tag.Get("form")
		// Allow ignored fields in the struct
		if fieldName == "-" {
			continue
		}

		if errs[0].FieldNames[0] == field.Name {
			data["Err_"+field.Name] = true

			trName := field.Tag.Get("locale")
			if len(trName) == 0 {
				trName = l.Tr("form." + field.Name)
			} else {
				trName = l.Tr(trName)
			}

			switch errs[0].Classification {
			case binding.ERR_REQUIRED:
				data["ErrorMsg"] = trName + l.Tr("form.require_error")
			case binding.ERR_ALPHA_DASH:
				data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error")
			case binding.ERR_ALPHA_DASH_DOT:
				data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error")
			case binding.ERR_SIZE:
				data["ErrorMsg"] = trName + l.Tr("form.size_error", GetSize(field))
			case binding.ERR_MIN_SIZE:
				data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinSize(field))
			case binding.ERR_MAX_SIZE:
				data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMaxSize(field))
			case binding.ERR_EMAIL:
				data["ErrorMsg"] = trName + l.Tr("form.email_error")
			case binding.ERR_URL:
				data["ErrorMsg"] = trName + l.Tr("form.url_error")
			default:
				data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification
			}
			return errs
		}
	}
	return errs
}
예제 #2
0
파일: form.go 프로젝트: Unknwon/orbiter
func Validate(errs binding.Errors, data map[string]interface{}, f Form) binding.Errors {
	if errs.Len() == 0 {
		return errs
	}

	data["HasError"] = true
	AssignForm(f, data)

	typ := reflect.TypeOf(f)
	val := reflect.ValueOf(f)

	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
		val = val.Elem()
	}

	for i := 0; i < typ.NumField(); i++ {
		field := typ.Field(i)

		fieldName := field.Tag.Get("form")
		// Allow ignored fields in the struct
		if fieldName == "-" {
			continue
		}

		if errs[0].FieldNames[0] == field.Name {
			data["Err_"+field.Name] = true

			name := field.Tag.Get("name")

			switch errs[0].Classification {
			case binding.ERR_REQUIRED:
				data["ErrorMsg"] = name + " cannot be empty."
			case binding.ERR_ALPHA_DASH:
				data["ErrorMsg"] = name + " must be valid alpha or numeric or dash(-_) characters."
			case binding.ERR_ALPHA_DASH_DOT:
				data["ErrorMsg"] = name + " must be valid alpha or numeric or dash(-_) or dot characters."
			case binding.ERR_SIZE:
				data["ErrorMsg"] = name + " must be size " + GetSize(field)
			case binding.ERR_MIN_SIZE:
				data["ErrorMsg"] = name + " must contain at least " + GetMinSize(field) + " characters."
			case binding.ERR_MAX_SIZE:
				data["ErrorMsg"] = name + " must contain at most " + GetMaxSize(field) + " characters."
			case binding.ERR_EMAIL:
				data["ErrorMsg"] = name + " is not a valid email address."
			case binding.ERR_URL:
				data["ErrorMsg"] = name + " is not a valid URL."
			case binding.ERR_INCLUDE:
				data["ErrorMsg"] = name + " must contain substring '" + GetInclude(field) + "'."
			default:
				data["ErrorMsg"] = "Unknown error: " + errs[0].Classification
			}
			return errs
		}
	}
	return errs
}