Exercise 21: Validating Forms - jkneal/spring-angular-train GitHub Wiki
Goals
Learn how to create formatters. Learn how to validate forms and display errors.
Instructions
- In
edu.train.shop, create a new formatter named 'CurrencyFormatter' that formats BigDecimal. For implementing the formatting, use the Java NumberFormat utility defined as follows:
protected NumberFormat getNumberFormat(Locale locale) {
DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
format.setParseBigDecimal(true);
format.setMaximumFractionDigits(2);
format.setMinimumFractionDigits(2);
format.setCurrency(Currency.getInstance("USD"));
return format;
}
-
Now register your new formatter (add as a bean)
-
In
src/main/resources/shop/index.html, within the products table, modify the price expression so that it uses the Spring conversion service -
In
edu.train.product.Product, add the following field validations:
| Field | Validation |
|---|---|
| price | not null |
| name | min size of 1 and max size of 20 |
| price | max integer digits of 19 and max fractional digits of 2 |
-
In
src/main/resources, create a new file named 'ValidationMessages.properties' -
Now add the following custom validation messages (for the validations in step 3, make sure to specify the custom messages on the annotation):
| Validation | Message Key | Message |
|---|---|---|
| not null | error.required | Value for {0} is required |
| size | error.maxSize | Value for {0} is not the correct length |
| digits | error.maxDigits | Value for {0} has more digits than the maximum allowed |
-
Now in
edu.train.shop.ShopController, modify the methodaddProductto take a second argument of typeorg.springframework.validation.BindingResultand named 'result'. Indicate that the product argument should be validated. Only save the product and redirect if there are no errors, otherwise return back to the add product view -
In `src/main/resources/templates/shop/add-product.html', add the CSS class 'error' to the inputs for fields 'name' and 'price' if those fields have errors
-
Above the label for the
nameinput field, if there are errors display all the error messages in a paragraph. Add the CSS class 'errors' to the paragraph tag
VERIFICATION
-
Start the training application and go to the app home page
-
Click on the link 'Ex 21 Formatter'. Verify all the prices in the products table are formatted as currencies (should have '$')
-
Now go back to the menu and click on the link 'Ex 21 Validate'. Now click 'Add' without entering any data. Verify the product name and price fields are highlighted with a red border. Also verify the messages "Value for price is required" and "Value for name is not the correct length" display above the name field
-
Now enter a value for the name field, and enter 19.998 for the price. Next click 'Add'. Verify the price field is highlighted, and the message "Value for price has more digits than the maximum allowed" displays
-
Finally enter a name, and '19.99' for the price. Verify the product is added successfully and you are returned to the shop page