The contact route handler

The ContactHandler is responsible for rendering the contact page on IGWEB, where the contact form will reside:

func ContactHandler(env *common.Env) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

We declare and initialize the formParams variable to a newly initialized FormParams instance, providing the values for the ResponseWriter and Request fields:

    formParams := isokit.FormParams{ResponseWriter: w, Request: r}

We then declare and initialize the contactForm variable, with a newly created ContactForm instance, by calling the NewContactForm function and passing in the reference to the formParams struct:

    contactForm := forms.NewContactForm(&formParams)

We switch on the type of HTTP request method:

    switch r.Method {

case "GET":
DisplayContactForm(env, contactForm)
case "POST":
validationResult := contactForm.Validate()
if validationResult == true {
submissions.ProcessContactForm(env, contactForm)
DisplayConfirmation(env, w, r)
} else {
DisplayContactForm(env, contactForm)
}
default:
DisplayContactForm(env, contactForm)
}

})
}

In the case that the HTTP request method is GET, we call the DisplayContactForm function, passing in the env object and the contactForm object. The DisplayContactForm function will render the contact form on the contact page.

In the case that the HTTP request method is a POST, we validate the contact form. Remember that if the /contact route is accessed using the POST method, it is indicative of the user having submitted the contact form to the route. We declare and initialize the validationResult variable, setting it to the value of the result from calling the Validate method of the ContactForm object, contactForm.

If the value of the validationResult is true, the form validated successfully. We call the ProcessContactForm function in the submissions package, passing in the env object and the ContactForm object. The ProcessContactForm function is responsible for handling a successful contact form submission. We then call the DisplayConfirmation function, passing in the env object, http.ResponseWriter, w, and *http.Request, r.

If the value of the validationResult is false, flow of control goes inside the else block, and we call the DisplayContactForm function passing in the env object and the ContactForm object, contactForm. This will render the contact form again, and this time, the user will see error messages pertaining to the fields that were either not filled out or that were not filled out properly.

In the case that the HTTP request method is neither a GET or a POST, we reach the default condition and simply call the DisplayContactForm function to display the contact form.

Here's the DisplayContactForm function:

func DisplayContactForm(env *common.Env, contactForm *forms.ContactForm) {
templateData := &templatedata.Contact{PageTitle: "Contact", Form: contactForm}
env.TemplateSet.Render("contact_page", &isokit.RenderParams{Writer: contactForm.FormParams().ResponseWriter, Data: templateData})
}

The function takes in an env object and a ContactForm object as input arguments. We start out by declaring and initializing the variable templateData, which will serve as the data object that we will be feeding to the contact_page template. We create a new instance of a templatedata.Contact struct and populate its PageTitle field to "Contact", and its Form field to the ContactForm object that was passed into the function.

Here's what the Contact struct from the templatedata package looks like:

type Contact struct {
PageTitle string
Form *forms.ContactForm
}

The PageTitle field represents the page title for the web page, and the Form field represents the ContactForm object.

We then call the Render method on the env.TemplateSet object, and we pass in the name of the template we wish to render, contact_page, along with the isomorphic template render parameters (RenderParams) object. We have assigned the Writer field, of the RenderParams object, with the ResponseWriter associated with the ContactForm object, and we have assigned the Data field with the templateData variable.

Here's the DisplayConfirmation function:

func DisplayConfirmation(env *common.Env, w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/contact-confirmation", 302)
}

This function is responsible for performing the redirect to the confirmation page. In this function, we simply call the Redirect function available in the http package and perform a 302 status redirect to the /contact-confirmation route.

Now that we've covered the route handler for the Contact page, it's time to take a look at the route handler for the contact form confirmation web page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.19.185