// InputNamed takes a (generally) user-provided input string and parses it into the out // parameter, which must be settable (this usually means you'll have to pass a pointer // to this function). See the documentation on Parse() for a list of the supported types. // If name is provided, it will be included in any error returned by this function. // Additional constraints might be specified with the tag parameter, the ones currently // supported are: // - required: Marks the field as required, will return an error if it's empty. // - optional: Marks the field as optional, will accept empty values. // - max_length: Sets the maximum length for the input. // - min_length: Sets the minimum length for the input. // - alphanumeric: Requires the input to be only letters and numbers // // Finally, the required parameter indicates if the value should be considered required // or optional in absence of the "required" and "optional" tag fields. func InputNamed(name string, input string, out interface{}, tag *structs.Tag, required bool) error { v, err := types.SettableValue(out) if err != nil { return err } if err := parse(input, v); err != nil { return err } if v.Type().Kind() != reflect.Bool && tag != nil { if ((required && !tag.Optional()) || tag.Required()) && input == "" { return RequiredInputError(name) } if maxlen, ok := tag.MaxLength(); ok && len(input) > maxlen { if name != "" { return i18n.Errorfc("form", "%s is too long (maximum length is %d)", name, maxlen) } return i18n.Errorfc("form", "too long (maximum length is %d)", maxlen) } if minlen, ok := tag.MinLength(); ok && len(input) < minlen { if name != "" { return i18n.Errorfc("form", "%s is too short (minimum length is %d)", name, minlen) } return i18n.Errorfc("form", "too short (minimum length is %d)", minlen) } if tag.Alphanumeric() && len(input) > 0 && !alphanumericRe.MatchString(input) { if name != "" { return i18n.Errorfc("form", "%s must be alphanumeric", name) } return i18n.Errorfc("form", "must be alphanumeric") } } return nil }
// Parse tries to parse an string into the given argument. // e.g. // var f float32 // Parse("27.5", &f) // var width uint // Parse("57", &width) // Supported types are: string, bool, u?int(8|16|32|64)? and float(32|64). If // the parsed value would overflow the given type, the maximum value // (or minimum, if it's negative) for the type will be set. // If arg implements the Parser interface, its Parse method will // be used instead. func Parse(val string, arg interface{}) error { if parser, ok := arg.(Parser); ok { return parser.Parse(val) } v, err := types.SettableValue(arg) if err != nil { return err } return parse(val, v) }
func (f *Form) appendVal(val interface{}) error { v, err := types.SettableValue(val) if err != nil { return err } s, err := structs.New(val, formTags, formStructConfigurator{}) if err != nil { return err } f.values = append(f.values, v) f.structs = append(f.structs, s) return nil }