Express Validations

 

Objectives

Generate error messages based on whether or not data was supplied in a form submit, checking req.body to do so.

Prevent duplicate data from being added into the database. Generate error messages if certain data submitted through a form already exists in the database.

Use an error array to contain only the errors that apply during a form submission.

Complete the exercise to demonstrate your ability to do the objectives above

Understand error handling using next(err)

Validating that a form has been filled out correctly with an error array

 

routes/index.js

views/index.hbs

The errors array will contain the correct errors because you pushed only the errors that applied

Validations with Knex

Validations with Knex

Check if the name exists in the database, if knex returns data to you, re-render the new.hbs file with an error message.  
Otherwise, you can insert the form data (req.body)

Database error message

Combining form validation and Knex validation

Combining form validation and Knex validation

All errors render correctly

When making post requests, res.render is usually used when re-rendering a page and passing in error messages

Text

res.render vs res.redirect with validations

res.render vs res.redirect with validations

Text

When making post requests, res.redirect is generally used after inserting data into a database, and then redirecting to the page that displays all of the database items

Error handling with next(err)

If you return next(new Error("some error"))

Express will render the error.hbs file and populate it with the error message you specified

Objectives

Generate error messages based on whether or not data was supplied in a form submit, checking req.body to do so.

Use an error array to contain only the errors that apply during a form submission.

Use an error array to contain only the errors that apply during a form submission.

Complete exercise to demonstrate your ability to do the objectives above

Prevent duplicate data from being added into the database. Generate error messages if data submitted through a form already exists in the database.

Generate success message on successful database insertion

Extra Challenge

Answer questions in WRITEHERE.md before submitting a pull request

Understand error handling using next(err)

Exercise

https://github.com/gSchool/expressValidations

Fork and clone the repo above

Follow the setup instructions

Use the directions and wireframes to complete all of the stories before you turn it in

Make a pull request to turn it in

A link to the slides is available in the repo for reference

Answer questions in the WRITEHERE.md file

Express Validations

By Akyuna Akish

Express Validations

  • 1,169