Validating Required Fields

Learn how to validate required fields in a Spring MVC application.

A required filed is a mandatory field that must be filled in before the form is submitted. In this lesson, we will add a required field to our form. The add-player-form has a textbox for name that stores data in the lastName field of the Athlete class. We will change this field and split it into first name and last name, with the latter being a required field. The first name can be left blank.

When the user clicks on the ”Add player” button, the request goes to the AthleteController class which performs validation check and based on the results, either sends the user to the player-confirmation page or back to the add-player-form for resubmission.

First, we will add the field firstName to the Athlete class along with its getter and setter methods as follows:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.