示例#1
0
文件: util.go 项目: rainycape/gondola
func validateNewEmail(ctx *app.Context, email string) (string, error) {
	addr, err := mail.Validate(email, true)
	if err != nil {
		return "", i18n.Errorf("this does not look like a valid email address")
	}
	userType := getUserType(ctx)
	found, err := ctx.Orm().Exists(ctx.Orm().TypeTable(userType), ByEmail(addr))
	if err != nil {
		return "", err
	}
	if found {
		return "", i18n.Errorf("email %q is already in use", addr)
	}
	return addr, nil
}
示例#2
0
文件: form.go 项目: rainycape/gondola
func (f *Form) validate() {
	if err := f.addCSRF(); err != nil {
		panic(err)
	}
	for _, v := range f.fields {
		inp := f.ctx.FormValue(v.HTMLName)
		label := v.Label.TranslatedString(f.ctx)
		if f.NamelessErrors {
			label = ""
		}
		if v.Type.HasChoices() {
			if inp == NotChosen {
				v.err = i18n.Errorfc("form", "You must choose a value").Err(f.ctx)
				continue
			}
			// Verify that the input mathces one of the available choices
			choices := f.fieldChoices(v)
			found := false
			for _, c := range choices {
				if inp == toHTMLValue(c.Value) {
					found = true
					break
				}
			}
			if !found {
				v.err = i18n.Errorfc("form", "%v is not a valid choice", inp).Err(f.ctx)
				continue
			}
		}
		if v.Type == FILE {
			file, header, err := f.ctx.R.FormFile(v.HTMLName)
			if err != nil && !v.Tag().Optional() {
				v.err = input.RequiredInputError(label)
				continue
			}
			if file != nil && header != nil {
				value := &formFile{
					file:   file,
					header: header,
				}
				v.value.Set(reflect.ValueOf(value))
			}
		} else {
			if err := input.InputNamed(label, inp, v.SettableValue(), v.Tag(), true); err != nil {
				v.err = i18n.TranslatedError(err, f.ctx)
				continue
			}
			if v.Type == EMAIL && !v.Tag().Has("novalidate") {
				// Don't validate empty emails. If we reached this point
				// with an empty one, the field is optional.
				if email, ok := v.Value().(string); ok && email != "" {
					if _, err := mail.Validate(email, true); err != nil {
						v.err = i18n.Errorfc("form", "%q is not a valid email address", email).Err(f.ctx)
						continue
					}
				}
			}
		}
		if err := structs.Validate(v.sval.Addr().Interface(), v.Name, f.ctx); err != nil {
			v.err = i18n.TranslatedError(err, f.ctx)
			continue
		}
	}
}