Skip to content
Advertisement

Input validation doesn’t work in Spring + Thymeleaf

I am new to spring and I explore it using “Spring in action 5” book. I do similar actions with my website, I have written 2 controllers and 2 html files for pages. The first page is a form for pizza creation. the second is order submission. My code for controllers is practically the same as in the book. Everything worked fine until I started to validate form inputs. I did everything that needed in Pizza, Order classes and controllers.

Order class:

JavaScript

Pizza class:

JavaScript

Controller for pizza creation:

JavaScript

Controller for the form submission:

JavaScript

The problem is in validating pizza creation. When I enter invalid name of the pizza and click button to submit my pizza I get Whitelabel Error Page.

JavaScript

It seems like I am not working properly with attributes in my CreatePizzaController class, it must be broken, though I’ll give html code below. I tried to replace Errors parameter in processCreation method to BindingResult parameter and check for erros but it works the same. Please help me, I don’t really understand why my code doesn’t work with input validation.

orderForm.html:

JavaScript

create.html:

JavaScript

P.S Adding @ModelAttribute annotation worked, my problem is solved. But now I get another problem related to thymeleaf. When I add thymeleaf code to handle input error and I try to enter invalid name of pizza, thymeleaf just doesn’t generate my checkboxes. Here is create.html:

JavaScript

Everything is fine until I click submit button with invalid name field. After that my error message appears, there are no exceptions as well as my checkboxes. this is how it looks like
and this is generated html code

Advertisement

Answer

Use the @ModelAttribute annotation:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement