Table of Contents
The minimal JavaScript front-end web application that we have discussed in Chapter 3 has been limited to support the minimum functionality of a data management app only. For instance, it did not take care of preventing the user from entering invalid data into the app's database. In this chapter, we show how to express integrity constraints in a JavaScript model class, and how to perform constraint validation both in the model part of the app and in the user interface built with HTML5.
We show how to perform responsive validation with the HTML5
form validation API. Since using the HTML5 input
field
types and validation attributes (like required
,
min
, max
and pattern
) implies
defining constraints in the UI, they are not really useful in a
best-practice approach where constraints are only checked, but not
defined, in the UI.
Consequently, we will not use the HTML5 features for defining constraints in the UI, but only use two methods of the HTML5 form validation API:
setCustomValidity
, which allows to mark a form
field as either valid or invalid by assigning either an empty string
or a non-empty (constraint violation) message string;
checkValidity
, which is invoked on a form before
user input data is committed or saved (for instance with a form
submission); it tests, if all fields have a valid value.
In the case of two special kinds of attributes, having
calendar dates or colors as
values, it is desirable to use the new UI widgets defined by HTML5 for
picking a date or picking a color (with corresponding input
field types).