Server side form validation 2

This commit is contained in:
Muyao CHEN
2024-06-30 19:35:59 +02:00
parent 8394832428
commit 8be6ba7119
4 changed files with 52 additions and 3 deletions

View File

@ -22,5 +22,14 @@ func New(data url.Values) *Form {
// Has checks if form field is in post and not emtpy
func (f *Form) Has(field string, r *http.Request) bool {
x := r.Form.Get(field)
return x != ""
if x == "" {
f.Errors.Add(field, "This field cannot be blank")
return false
}
return true
}
// Valid returns true if there are no errors, otherwise false
func (f *Form) Valid() bool {
return len(f.Errors) == 0
}