Ver código fonte

Allow FormField.Name to match POST form field name.

Bozhin Zafirov 2 anos atrás
pai
commit
a8482ee0e2
1 arquivos alterados com 8 adições e 4 exclusões
  1. 8 4
      validate.go

+ 8 - 4
validate.go

@@ -25,12 +25,16 @@ func ValidateForm(r *http.Request, p interface{}) error {
 	for HttpFormField, HttpFormValue := range r.Form {
 		for n := 0; n < formStruct.NumField(); n++ {
 			fieldt := formStruct.Type().Field(n)
-			/* only proceed if field name or tag matches that of form field */
-			if fieldt.Name != HttpFormField && fieldt.Tag.Get("form") != HttpFormField {
-				continue
-			}
 			/* get n-th field */
 			fieldn := formStruct.Field(n)
+			/* only proceed if field name or tag matches that of form field */
+			if fieldn.Field(0).String() != HttpFormField {
+				if fieldt.Name != HttpFormField {
+					if fieldt.Tag.Get("form") != HttpFormField {
+						continue
+					}
+				}
+			}
 			/* set form data to field
 			   equivalent of form.Value = HttpFormValue[0] */
 			fieldn.Field(1).Set(reflect.ValueOf(HttpFormValue[0]))