Skip to the content.

nextjs-example-form is a sample Next.js 14 application that illustrates how to use React Hook Form and Yup for form development and validation.

Some features of this example:

Installation

First, install PostgreSQL. Then create a database for your application.


$ createdb nextjs-example-form
Password:
$

Second, download this repository to your computer. You can download it as a zip file, or you can click the “Use as template” button to create your own copy of the system, then clone it to your local computer.

Third, go to your newly created repository, and click the “Clone or download” button to download your new GitHub repo to your local file system. Using GitHub Desktop is a great choice if you use MacOS or Windows.

Fourth, cd into the directory of your local copy of the repo, and install third party libraries with:

$ npm install

Fifth, create a .env file from the sample.env. Set the DATABASE_URL variable to match your PostgreSQL database you created in the first step. See the Prisma docs Connect your database. Then run the Prisma migration npx prisma migrate dev to set up the PostgreSQL tables.

$ npx prisma migrate dev
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "<your database name>", schema "public" at "localhost:5432"

Applying migration `20240702013420_init`
Applying migration `20240702172401_unique_email`

The following migration(s) have been applied:

migrations/
  └─ 20240702013420_init/
    └─ migration.sql
  └─ 20240702172401_unique_email/
    └─ migration.sql

Your database is now in sync with your schema.

✔ Generated Prisma Client (v5.16.1) to ./node_modules/@prisma/client in 51ms

$

Running the system

Once the libraries are installed and the database is migrated, you can run the application by invoking the “dev” script in the package.json file:

$ npm run dev

> nextjs-example-form@0.1.0 dev
> next dev

  ▲ Next.js 14.2.4
  - Local:        http://localhost:3000
  - Environments: .env

 ✓ Starting...
 ✓ Ready in 2.6s

Viewing the running app

If all goes well, the application will appear at http://localhost:3000.

ESLint

You can verify that the code obeys our coding standards by running ESLint over the code in the imports/ directory with:

$ npm run lint

> nextjs-example-form3@0.1.0 lint
> next lint

✔ No ESLint warnings or errors

If you see a Typescript warning you can ignore it.

Prerequisites

To best understand this application, it is useful to familiarize yourself with:

Walkthrough

The landing page for this application provides the Create Student form:

This form has the following input controls:

A filled out but not yet submitted Create Student form looks like this:

After submission, the page pops up an alert showing the submission was successful:

Also note that after dismissing the alert, there is a link of the Create Student page to a page where you can edit the document. Here is this page:

You can edit the fields, then click ‘Update’ to save the changes.