Эх сурвалжийг харах

simplify FormField by removing Label and Type properties

Bozhin Zafirov 8 жил өмнө
parent
commit
01665e4e3e
3 өөрчлөгдсөн 7 нэмэгдсэн , 9 устгасан
  1. 5 5
      constructors.go
  2. 0 2
      forms.go
  3. 2 2
      validate.go

+ 5 - 5
constructors.go

@@ -1,14 +1,14 @@
 package form
 
 // Generate new CharField field with type text
-func NewCharField(Name, Label string, Value *string, Validators *ValidatorsList) FormField {
+func NewCharField(Name string, Value *string, Validators *ValidatorsList) FormField {
 	if Value == nil {
-		return FormField{Name, "text", Label, "", nil, Validators}
+		return FormField{Name, "", nil, Validators}
 	}
-	return FormField{Name, "text", Label, *Value, nil, Validators}
+	return FormField{Name, *Value, nil, Validators}
 }
 
 // Generate new CharField field with type password
-func NewPasswordField(Name, Label string, Validators *ValidatorsList) FormField {
-	return FormField{Name, "password", Label, "", nil, Validators}
+func NewPasswordField(Name string, Validators *ValidatorsList) FormField {
+	return FormField{Name, "", nil, Validators}
 }

+ 0 - 2
forms.go

@@ -14,8 +14,6 @@ type ValidatorsList []ValidatorFunc
 // A general purpose form  field struct
 type FormField struct {
 	Name       string
-	Type       string
-	Label      string
 	Value      string
 	Error      error
 	Validators *ValidatorsList

+ 2 - 2
validate.go

@@ -33,7 +33,7 @@ func ValidateForm(r *http.Request, p interface{}) error {
 			fieldn := formStruct.Field(n)
 			// set form data to field
 			// equivalent of form.Value = HttpFormValue[0]
-			fieldn.Field(3).Set(reflect.ValueOf(HttpFormValue[0]))
+			fieldn.Field(1).Set(reflect.ValueOf(HttpFormValue[0]))
 		}
 	}
 
@@ -46,7 +46,7 @@ func ValidateForm(r *http.Request, p interface{}) error {
 		}
 		for _, validator := range *field.Validators {
 			if err := validator(field, r.Context()); err != nil {
-				fieldn.Field(4).Set(reflect.ValueOf(err))
+				fieldn.Field(2).Set(reflect.ValueOf(err))
 				FormError = err
 				break
 			}