github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section4/gopherfaceform/handlers/signup.go (about)

     1  package handlers
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/EngineerKamesh/gofullstack/volume2/section4/gopherfaceform/validationkit"
     7  )
     8  
     9  type SignUpForm struct {
    10  	FieldNames []string
    11  	Fields     map[string]string
    12  	Errors     map[string]string
    13  }
    14  
    15  // DisplaySignUpForm displays the Sign Up form
    16  func DisplaySignUpForm(w http.ResponseWriter, r *http.Request, s *SignUpForm) {
    17  	RenderTemplate(w, "./templates/signupform.html", s)
    18  }
    19  
    20  func DisplayConfirmation(w http.ResponseWriter, r *http.Request, s *SignUpForm) {
    21  	RenderUnsafeTemplate(w, "./templates/signupconfirmation.html", s)
    22  }
    23  
    24  func PopulateFormFields(r *http.Request, s *SignUpForm) {
    25  
    26  	for _, fieldName := range s.FieldNames {
    27  		s.Fields[fieldName] = r.FormValue(fieldName)
    28  	}
    29  
    30  }
    31  
    32  // ValidateSignUpForm validates the Sign Up form's fields
    33  func ValidateSignUpForm(w http.ResponseWriter, r *http.Request, s *SignUpForm) {
    34  
    35  	PopulateFormFields(r, s)
    36  	// Check if username was filled out
    37  	if r.FormValue("username") == "" {
    38  		s.Errors["usernameError"] = "The username field is required."
    39  	}
    40  
    41  	// Check if first name was filled out
    42  	if r.FormValue("firstName") == "" {
    43  		s.Errors["firstNameError"] = "The first name field is required."
    44  	}
    45  
    46  	// Check if last name was filled out
    47  	if r.FormValue("lastName") == "" {
    48  		s.Errors["lastNameError"] = "The last name field is required."
    49  	}
    50  
    51  	// Check if e-mail address was filled out
    52  	if r.FormValue("email") == "" {
    53  		s.Errors["emailError"] = "The e-mail address field is required."
    54  	}
    55  
    56  	// Check if e-mail address was filled out
    57  	if r.FormValue("password") == "" {
    58  		s.Errors["passwordError"] = "The password field is required."
    59  	}
    60  
    61  	// Check if e-mail address was filled out
    62  	if r.FormValue("confirmPassword") == "" {
    63  		s.Errors["confirmPasswordError"] = "The confirm password field is required."
    64  	}
    65  
    66  	// Check username syntax
    67  	if validationkit.CheckUsernameSyntax(r.FormValue("username")) == false {
    68  
    69  		usernameErrorMessage := "The username entered has an improper syntax."
    70  		if _, ok := s.Errors["usernameError"]; ok {
    71  			s.Errors["usernameError"] += " " + usernameErrorMessage
    72  		} else {
    73  			s.Errors["usernameError"] = usernameErrorMessage
    74  		}
    75  	}
    76  
    77  	// Check e-mail address syntax
    78  	if validationkit.CheckEmailSyntax(r.FormValue("email")) == false {
    79  		emailErrorMessage := "The e-mail address entered has an improper syntax."
    80  		if _, ok := s.Errors["usernameError"]; ok {
    81  			s.Errors["emailError"] += " " + emailErrorMessage
    82  		} else {
    83  			s.Errors["emailError"] = emailErrorMessage
    84  		}
    85  	}
    86  
    87  	// Check if passord and confirm password field values match
    88  	if r.FormValue("password") != r.FormValue("confirmPassword") {
    89  		s.Errors["confirmPasswordError"] = "The password and confirm pasword fields do not match."
    90  	}
    91  
    92  	if len(s.Errors) > 0 {
    93  		DisplaySignUpForm(w, r, s)
    94  	} else {
    95  		ProcessSignUpForm(w, r, s)
    96  	}
    97  
    98  }
    99  
   100  // ProcessSignUpForm
   101  func ProcessSignUpForm(w http.ResponseWriter, r *http.Request, s *SignUpForm) {
   102  
   103  	// If we reached this point, that indicates that we had a successful form submission.
   104  	// Later, we will include form processing logic here, in this case that would be
   105  	// inserting the information from the form as an entry into the database.
   106  
   107  	// Display form confirmation message
   108  	DisplayConfirmation(w, r, s)
   109  
   110  }
   111  
   112  func SignUpHandler(w http.ResponseWriter, r *http.Request) {
   113  
   114  	s := SignUpForm{}
   115  	s.FieldNames = []string{"username", "firstName", "lastName", "email"}
   116  	s.Fields = make(map[string]string)
   117  	s.Errors = make(map[string]string)
   118  
   119  	switch r.Method {
   120  
   121  	case "GET":
   122  		DisplaySignUpForm(w, r, &s)
   123  	case "POST":
   124  		ValidateSignUpForm(w, r, &s)
   125  	default:
   126  		DisplaySignUpForm(w, r, &s)
   127  	}
   128  
   129  }