Server Side form validation 4: check email
This commit is contained in:
@ -5,6 +5,8 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/asaskevich/govalidator"
|
||||
)
|
||||
|
||||
// Form creates a custom form struct, embeds a url.Values object
|
||||
@ -51,3 +53,11 @@ func (f *Form) MinLength(field string, length int, r *http.Request) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// IsEmail checks the email address
|
||||
func (f *Form) IsEmail(field string, r *http.Request) {
|
||||
value := r.Form.Get(field)
|
||||
if !govalidator.IsEmail(value) {
|
||||
f.Errors.Add(field, "Invalid email address")
|
||||
}
|
||||
}
|
||||
|
@ -101,6 +101,7 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
form.Required("first_name", "last_name", "email")
|
||||
form.MinLength("first_name", 2, r)
|
||||
form.IsEmail("email", r)
|
||||
|
||||
if !form.Valid() {
|
||||
data := make(map[string]interface{})
|
||||
|
Reference in New Issue
Block a user