package form import ( "database/sql/driver" "fmt" "html/template" "net/http" "strconv" "strings" "time" ) type Attribute struct { Key, Val string } type InputField struct { Name string Label string Type string Val string Is string Required bool Attributes []template.HTMLAttr Errors []error } func (field *InputField) Scan(value interface{}) error { if value == nil { field.Val = "" return nil } switch v := value.(type) { case time.Time: if field.Type == "date" { field.Val = v.Format("2006-01-02") } else if field.Type == "time" { field.Val = v.Format("15:04") } else { field.Val = v.Format(time.RFC3339) } default: field.Val = fmt.Sprintf("%v", v) } return nil } func (field *InputField) Value() (driver.Value, error) { return field.Val, nil } func (field *InputField) FillValue(r *http.Request) { field.Val = strings.TrimSpace(r.FormValue(field.Name)) } func (field *InputField) Integer() int { value, err := strconv.Atoi(field.Val) if err != nil { panic(err) } return value } func (field *InputField) IntegerOrNil() interface{} { if field.Val != "" { if i := field.Integer(); i > 0 { return i } } return nil } func (field *InputField) Float64() float64 { value, err := strconv.ParseFloat(field.Val, 64) if err != nil { panic(err) } return value } func (field *InputField) String() string { return field.Val }