Add form exist #67
15
form.go
15
form.go
|
@ -18,11 +18,19 @@ type Forms http.Request
|
|||
|
||||
var _ Set = &Forms{}
|
||||
|
||||
var ErrKeyNotExist = errors.New("not exist")
|
||||
|
||||
// Values returns http.Request values
|
||||
func (f *Forms) Values() url.Values {
|
||||
return (*http.Request)(f).Form
|
||||
}
|
||||
|
||||
// Exist if the key exist in forms
|
||||
func (f *Forms) Exist(key string) bool {
|
||||
_, err := f.Strings(key)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// String returns request form as string
|
||||
func (f *Forms) String(key string) (string, error) {
|
||||
return (*http.Request)(f).FormValue(key), nil
|
||||
|
@ -41,7 +49,7 @@ func (f *Forms) Strings(key string) ([]string, error) {
|
|||
if v, ok := (*http.Request)(f).Form[key]; ok {
|
||||
return v, nil
|
||||
}
|
||||
return nil, errors.New("not exist")
|
||||
return nil, ErrKeyNotExist
|
||||
}
|
||||
|
||||
// Escape returns request form as escaped string
|
||||
|
@ -221,6 +229,11 @@ func (f *Forms) MustBool(key string, defaults ...bool) bool {
|
|||
return v
|
||||
}
|
||||
|
||||
// Exist if the key exist in forms
|
||||
func (ctx *Context) FormExist(key string) bool {
|
||||
return (*Forms)(ctx.req).Exist(key)
|
||||
}
|
||||
|
||||
// Form returns request form as string with default
|
||||
func (ctx *Context) Form(key string, defaults ...string) string {
|
||||
return (*Forms)(ctx.req).MustString(key, defaults...)
|
||||
|
|
Loading…
Reference in New Issue
Block a user