New Issues
Let’s learn about some of the issues that you’ll face with the new application.
We'll cover the following
Challenges of adding constraint validation
We have to deal with several new issues that were not present in the MinimalApp that we discussed before:
-
In the model code, for every property of a class we have to add the following:
- A
check
function that can be invoked for validating the constraints defined for the property. - A
setter
method that invokes the check function and is used to set the property’s value.
- A
-
In the user interface (“view”) code, we have to take care of the following:
- Responsive validation on user input for providing immediate feedback to the user.
- Validation on form submission for preventing the submission of flawed data to the model layer. For improving the break-down of the view code, we introduce a utility method (in
lib/util.js
) that fills a select form control with option elements, the contents of which are retrieved from an entity table such asBook.instances
. This method is used in thesetupUserInterface
method of both theupdateBook
and thedeleteBook
use cases.
Checking the constraints in the user interface (UI) on user input is important for providing immediate feedback to the user. However, it’s not safe enough to perform constraint validation only in the UI. This is mainly because it could be circumvented in a distributed web application, where the UI runs in the web browser of a front-end device, while a back-end component manages the application’s data on a remote web server.
Consequently, we need multiple constraint validation, first in the UI on input (or on change) and on form submission, and subsequently in the model layer before saving or sending data to the persistent data store. In an application based on a DBMS, we may also use a third round of validation before persistent storage by using the validation mechanisms of the DBMS. This is a must when the application’s database is shared with other applications.
Our proposed solution to this multiple validation problem is to keep the constraint validation code in special check functions in the model classes. We’ll invoke these functions both in the UI on user input and on form submission, as well as in the create and update data management methods of the model class via invoking the setters. Notice that referential integrity constraints,and other relationship constraints, may also be violated through a delete operation, but we don’t have to consider this in our single-class example.
Get hands-on with 1400+ tech skills courses.