How to display error message from custom validation on thymeleaf page - spring-boot

Similar question is posted here-> Spring + Thymeleaf custom validation display but I could not figure out the solution so posting this new question.
I have a simple registration form having username,email, password and confirmPassword fields
<form action="#" th:action="#{/register}" th:object="${user}"
method=post>
<!--error detection start -->
<div class="alert alert-danger" th:if="${#fields.hasErrors('*')}">
<p th:each="err : ${#fields.errors('*')}" th:text="${err}"></p>
</div>
<!--error detection ends -->
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text"> <i class="fa fa-user"></i>
</span>
</div>
<input name="username" th:field="*{username}" class="form-control"
placeholder="User Name" type="text">
</div>
<div class="form-group input-group"
th:if="${#fields.hasErrors('username')}" th:errors="*{username}">Name
Error</div>
<!-- form-group// -->
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text"> <i class="fa fa-envelope"></i>
</span>
</div>
<input name="email" th:field="*{email}" class="form-control"
placeholder="Email address" type="email">
</div>
<div class="form-group input-group"
th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Name
Error</div>
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text"> <i class="fa fa-lock"></i>
</span>
</div>
<input class="form-control" th:field="*{password}"
placeholder="Create password" type="password">
</div>
<!-- form-group// -->
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text"> <i class="fa fa-lock"></i>
</span>
</div>
<input class="form-control" th:field="*{confirmPassword}"
placeholder="Repeat password" type="password">
<p class="error-message"
th:each="error: ${#fields.errors('user.confirmPassword')}"
th:text="${error}">Validation error</p>
</div>
<!-- form-group// -->
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">
Create Account</button>
</div>
<!-- form-group// -->
<p class="text-center">
Have an account? Log In
</p>
</form>
I added a custom validation which gets trigger when password and confirm password field do not match.
1.FieldsValueMatchValidator
public class FieldsValueMatchValidator implements ConstraintValidator<FieldsValueMatch, Object> {
private String field;
private String fieldMatch;
public void initialize(FieldsValueMatch constraintAnnotation) {
this.field = constraintAnnotation.field();
this.fieldMatch = constraintAnnotation.fieldMatch();
}
public boolean isValid(Object value, ConstraintValidatorContext context) {
Object fieldValue = new BeanWrapperImpl(value).getPropertyValue(field);
Object fieldMatchValue = new BeanWrapperImpl(value).getPropertyValue(fieldMatch);
if (fieldValue != null) {
return fieldValue.equals(fieldMatchValue);
} else {
return fieldMatchValue == null;
}
}
}
3.FieldsValueMatch
#Constraint(validatedBy = FieldsValueMatchValidator.class)
#Target({ ElementType.TYPE })
#Retention(RetentionPolicy.RUNTIME)
public #interface FieldsValueMatch {
String message() default "Fields values don't match!";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String field();
String fieldMatch();
#Target({ ElementType.TYPE })
#Retention(RetentionPolicy.RUNTIME)
#interface List {
FieldsValueMatch[] value();
}
}
4.User.java
#FieldsValueMatch(field = "password", fieldMatch = "confirmPassword", message = "Passwords do not match!")
#Entity
public class User implements UserDetails
{
#NotBlank
private String password;
#Transient
#NotBlank
private String confirmPassword;
//other getters and setters
}
5.Controller code
#PostMapping("/register")
public String saveNonJsonData(#Valid #ModelAttribute("user") User theUser, BindingResult errors) {
if (errors.hasErrors()) {
return "register";
}
else
{
//successlogic
}
Custom validator is working fine and i can see the error message on the page using following code on thymeleaf page
<!--error detection start -->
<div class="alert alert-danger" th:if="${#fields.hasErrors('*')}">
<p th:each="err : ${#fields.errors('*')}" th:text="${err}"></p>
</div>
<!--error detection ends -->
As mentioned here - Spring + Thymeleaf custom validation display the problem is custom validator returning an ObjectError for password field match validation and not a fieldError. Even though I tried solution provided I can't figure out how to get Thymeleaf to display my custom error.
UPDATE
Got one more answer here Displaying "Passwords don't match" custom annotation message and now i can see the error message using following code
<input class="form-control" th:field="*{confirmPassword}" placeholder="Repeat password" type="password">
<div class="form-group input-group" th:if="${#fields.hasErrors('global')}" th:errors="*{global}"></div>
My updated question if I have two more fields for example 'email' and 'confirmEmail' field then how this approach will work on thymeleaf page?

Related

How do I validate an attribute of type BigDecimal using Bean Validation annotation?

I'm just wanting to do a simple validation, if the html fields below are sent "null or empty" the user can't persist the data. How can I solve this problem?
below my code
HTML:
<div th:each="msgErrors : ${erros}" class="alert alert-danger alert-dismissible fade show" role="alert">
<span th:text="${msgErrors}"></span>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form th:action="cadastrar" method="post" th:object="${fatura}">
<div class="row">
<div class="col-lg-4 col-sm-12">
<label for="cofins">COFINS:</label>
<input type="text" class="form-control" autocomplete="off" id="cofins" th:field="*{cofins}" placeholder="0,00 %">
</div>
<div class="col-lg-4 col-sm-12">
<label for="icms">ICMS:</label>
<input type="text" class="form-control" autocomplete="off" id="icms" th:field="*{icms}" placeholder="0,00 %">
</div>
</div>
<div class="row mt-4">
<button type="submit" class="btn btn-success mr-3"><i class="far fa-save"></i> SALVAR</button>
</div>
</form>
My Model Class:
#Entity
public class Fatura {
#NumberFormat(pattern = "#,###.##")
#Column(name = "cofins")
private BigDecimal cofins;
#NumberFormat(pattern = "#,###.##")
#Column(name = "icms")
private BigDecimal icms;
}
My Controller:
public ModelAndView cadastro(#Valid Fatura fatura, BindingResult br){
if(br.hasErrors()){
ModelAndView mv = new ModelAndView("cadastro/cadastro");
mv.addObject("fatura", fatura);
mv.addObject("listLojas", Lojas.values());
List<String> msg = new ArrayList<String>();
for(ObjectError objError : br.getAllErrors()){
msg.add(objError.getDefaultMessage());
}
mv.addObject("erros", msg);
return mv;
}
I need the user to not be allowed to send the fields without typing anything
There are two methods you can use,
NotNull bean validation
Constraint in your database for the fields as not nullable
For more info about bean validation check this.

Model attribute inconsistent after form submission

I'm still trying to figure out what's happening on the backend here. Working with Spring Boot/Thymeleaf. I have a form template that updates a model attribute object on save. I'm trying to use the updated values on the backend, however it's inconsistent among my functions and I'm not sure why.
Thymeleaf template
<div layout:fragment="content" class="container">
<form action="#" th:action="#{/utility/exportbadges}" th:object="${badgeExport}" method="post">
<div class="form-group col-md-6">
<label class="col-form-label-sm">Badge type</label>
<select th:field="*{type}" class="form-control">
<option th:each="badgeType : ${T(org.myorg.myproject.model.badge.BadgeType).values()}"
th:value="${badgeType}"
th:text="${badgeType}">
</option>
</select>
</div>
<div class="form-group col-md-3">
<label class="col-form-label-sm">Use background?</label>
<input type="checkbox" th:field="*{background}" class="form-control">
</div>
<div class="form-group col-md-3">
<label class="col-form-label-sm">Mark preprinted?</label>
<input type="checkbox" th:field="*{preprinted}" class="form-control">
</div>
<div class="form-group col-md-3">
<label class="col-form-label-sm">Save to path (/tmp default):</label>
<input type="text" th:field="*{saveDir}" class="form-control" placeholder="/tmp">
</div>
<div class="form-group col-md-12 mt-2">
<div class="col-sm-10">
<input class="btn btn-primary" id="save" type="submit" value="Export" />
<input class="btn btn-secondary" type="reset" value="Reset" />
</div>
</div>
</form>
</div>
#RequestMapping(value = "/utility/exportbadges")
public String exportBadges(Model model) {
final BadgeExport badgeExport = new BadgeExport();
model.addAttribute("badgeExport", badgeExport);
return "utility/exportbadges";
}
POST method. The object is correct in this function. Any field that's edited in the form above reflects in this function. However, on redirect, the object is as if it only has default instantiation/has been unedited.
#RequestMapping(value = "/utility/exportbadges", method = RequestMethod.POST)
public String exportBadgeFlow(Model model,
#ModelAttribute("badgeExport") final BadgeExport badgeExport) {
log.info("BadgeExport badge type: {}", badgeExport.getType());
log.info("BadgeExport save dir pre export: {}", badgeExport.getSaveDir());
switch(badgeExport.getType()) {
case "Attendee":
log.error("Attendee export not yet implemented");
break;
case "Vip":
return "redirect:exportbadges/vip-badges.pdf";
case "Specialty":
return "redirect:exportbadges/specialty-badges.pdf";
case "Staff":
return "redirect:exportbadges/staff-badges.pdf";
case "Guest":
return "redirect:exportbadges/guest-badges.pdf";
}
return "redirect:exportbadges";
}
Redirect function. Badge type will be null and saveDir will be /tmp as default instead of updated value in form.
#RequestMapping(value = "/utility/exportbadges/vip-badges.pdf")
public ResponseEntity<String> getAllVipBadgePdf(#ModelAttribute("badgeExport") final BadgeExport badgeExport) throws IOException {
log.info("BadgeExport badge type: {}", badgeExport.getType());
log.info("BadgeExport save dir during export: {}", badgeExport.getSaveDir());
}

Spring MVC; error messages of validation form not being displayed

I have built a form for login. I've read about validations with spring mvc following a tutorial.
The form is blank & doesn't pass values, however it doesn't show any error messages.
This is my code for the controller:
#Controller
public class LoginController {
#RequestMapping("/login")
public ModelAndView login() {
return new ModelAndView("login", "user", new UserModel());
}
#RequestMapping("/submitLogin")
public ModelAndView submitLogin(#Valid UserModel user, BindingResult result) {
ModelAndView model = new ModelAndView();
model.addObject("user", user);
model.setViewName(result.hasErrors() ? "login" : "index");
return model;
}
}
This is my model:
#Data
public class UserModel {
#NotBlank(message = "The field login name is required")
private String login;
#NotBlank(message = "The field password is required")
private String password;
}
And this is my form:
<mvc:form modelAttribute="user" class="col s12" action="/submitLogin" method="POST">
<div class="container login-form">
<div class="row">
<div class="input-field col s12">
<mvc:input path="login" id="userName" type="text" class="validate" />
<mvc:label path="login" for="userName">Login name</mvc:label>
</div>
</div>
<div class="row">
<mvc:errors path="login" cssStyle="color: #ff0000;"/>
</div>
<div class="row">
<div class="input-field col s12">
<mvc:input path="password" id="password" type="password" class="validate" />
<mvc:label path="password" for="password">Password</mvc:label>
</div>
</div>
<div class="row">
<mvc:errors path="password" cssStyle="color: #ff0000;"/>
</div>
<div class="row">
<input type="submit" class="btn waves-effect waves-light pulse green" value="Entrar"/>
</div>
</div>
</mvc:form>
Did I miss any configuration on spring boot or annotation?

How tell the view an error has occured from controller? (Spring MVC)

How can I say trigger error/validation messages in the view from the controller in a better way? Currently, I do this by sending boolean attributes. For example, in creating a product, I have two possible errors. Invalid format of UPC of a product, or duplicate upc. I also have a validati
#RequestMapping("/createProduct")
public String createProduct(Model model, #RequestParam(value = "name") String name,
#RequestParam(value = "upc") String upc, #RequestParam(value = "category") String categoryName,
#RequestParam(value = "description") String description, #RequestParam(value = "price") BigDecimal price,
#RequestParam(value = "stock") int stock){
model.addAttribute("activeTab", 3);
if(Validator.invalidUpcFormat(upc)){
model.addAttribute("invalidFormat", true); //trigger for invalid format
return "management";
}
Category category = productService.getCategory(categoryName);
Product product = new Product(upc, category, name, description, price);
InventoryProduct inventoryProduct = new InventoryProduct(product, stock);
try {
managerService.add(inventoryProduct);
model.addAttribute("productCreated", true);
} catch (DuplicateProductException e) {
model.addAttribute("upc", upc);
model.addAttribute("duplicateProduct", true); // trigger for duplicate product
}
return "management";
}
And here is my view:
<div id="menu3"
class="tab-pane fade <c:if test="${activeTab == 3}">in active</c:if>">
<div class="container-fluid" style="padding: 2%;">
<div class="row">
<div class="col-md-12"
style="padding-left: 15%; padding-right: 15%;">
<c:if test="${productCreated}">
<div class="alert alert-success fade in">
<a href="#" class="close" data-dismiss="alert"
aria-label="close">×</a> <strong>Success!</strong>
Product has been created!
</div>
</c:if>
<c:if test="${duplicateProduct}">
<div class="alert alert-warning fade in">
<a href="#" class="close" data-dismiss="alert"
aria-label="close">×</a> <strong>Oh no!</strong>
Product with the UPC ${upc} already exists!
</div>
</c:if>
<c:if test="${invalidFormat}">
<div class="alert alert-warning fade in">
<a href="#" class="close" data-dismiss="alert"
aria-label="close">×</a> <strong>Oops!</strong>
Invalid UPC format!
</div>
</c:if>
<form
action="${pageContext.request.contextPath}/manager/createProduct"
method="post">
<div class="form-group">
<label for="Name">Name </label> <input type="text" name="name"
class="form-control" required />
</div>
<div class="form-group">
<label for="UPC">UPC </label> <input type="number" name="upc"
class="form-control" required />
</div>
<div class="form-group">
<div class="form-group">
<label for="category">Category</label> <select
class="form-control" name="category" required>
<option selected disabled value="">SELECT CATEGORY</option>
<c:forEach items="${categories}" var="item">
<option>${item.getName()}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" rows="5" name="description"></textarea>
</div>
<div class="form-group">
<label for="price">Price </label> <input type="number"
name="price" class="form-control" required />
</div>
<div class="form-group">
<label for="stock">Stock </label> <input type="number"
name="stock" class="form-control" required />
</div>
<button type="submit" class="btn btn-primary">Add
product</button>
</form>
</div>
</div>
</div>
</div>
Is there a better of doing this other than sending boolean triggers?
You could use Spring BindingResult. This is typical filled with the result of Binding and Validation results. But you can also add errors by hand.
But first you need to refactor your code, so that you use an single command/form-backing object instead of all the #Param values
public class CreateProductCommand {
private String name;
private String upc;
private String categoryName;
.... //other fields
public CreateProductCommand (){} //parameter less conturctor
Getter+Setter
}
Controller
#RequestMapping("/createProduct")
public ModelAndView createProduct(CreateProductCommand createProductCommand, BindingResult bindingResult) //Binding result must be the parameter direct next to the object that should been validated!!!
{
if (someustomValidationForUcpFail()) {
bindingResult.rejectValue("upc", //the field name of the invalid field
"error.Message.Key",
"Default Error Message");
}
if (bindingResult.hasErrors()) {
ModelMap model = new ModelMap();
model.add("createProductCommand", createProductCommand);
return new ModelAndView("createForm", model)
} else {
Product product = ...
return new ModelAndView("showProduct", "product", product)
}
}
jsp:
You need to use springs form and input tag:
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:springForm="http://www.springframework.org/tags/form"
version="2.0">
....
<springForm:form action="<c:url value="/manager/createProduct">" method="POST" modelAttribute="createProductCommand">
<springForm:input path="name"/> <form:errors path="name" />
<springForm:input path="ucp"/> <form:errors path="ucp" />
....
</springForm:form>
....

Validation of wrapper in spring form

I've wrapped two objects I wanted to use in one form. Here is the wrapper class:
public class UserCustomer {
User user;
Customer customer;
//Getters and setters
//Constructors
Controller:
#Controller
public class RegisterController {
#RequestMapping("/register")
public String showRegister(Model model){
model.addAttribute("userCustomer", new UserCustomer(new User(), new Customer()));
return "register";
}
#RequestMapping(value="/doRegister", method = RequestMethod.POST)
public String doRegister(Model model, #Valid UserCustomer userCustomer, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return "register";
}
System.out.println(userCustomer.getUser());
return "registered";
}
}
And the form:
<h3 class="new-models">For New Customers</h3>
<div class="register">
<sf:form method="post" action="${pageContext.request.contextPath}/doRegister" commandName="userCustomer">
<div class="register-top-grid">
<h3>PERSONAL INFORMATION</h3>
<div>
<span>First Name<label>*</label></span>
<sf:errors path="customer.name" cssClass="alert-danger"/>
<sf:input name="name" type="text" path="customer.name"/>
</div>
<div>
<span>Last Name<label>*</label></span>
<sf:errors path="customer.surname" cssClass="alert-danger"/>
<sf:input name="surname" type="text" path="customer.surname"/>
</div>
<div>
<span>Email Address<label>*</label></span>
<sf:errors path="user.email" cssClass="alert-danger"/>
<sf:input name="email" type="text" path="user.email"/>
</div>
<div>
<span>Username<label>*</label></span>
<sf:errors path="user.username" cssClass="alert-danger"/>
<sf:input name="username" type="text" path="user.username"/>
</div>
<div class="clearfix"> </div>
<!--a class="news-letter" href="#">
<label class="sf:checkbox">
<!--sf:input type="checkbox" name="newsletter" path="customer.newsletter" checked=""/><i> </i>Sign Up for Newsletter</label>
</a-->
<a class="news-letter" href="#"></a>
</div>
<div class="register-bottom-grid">
<h3>LOGIN INFORMATION</h3>
<div>
<span>Password<label>*</label></span>
<sf:errors path="user.password" cssClass="alert-danger"/>
<sf:input name="password" type="password" path="user.password"/>
</div>
<div>
<span>Confirm Password<label>*</label></span>
<sf:input type="password" path=""/>
</div>
</div>
<div class="clearfix"> </div>
<div class="register-but">
<input type="submit" value="register">
<div class="clearfix"> </div>
</div>
</sf:form>
</div>
Everything seems to be working fine, except for validation. I don't really know how to make validation in this wrapper.
Example validation for email in User class:
#Pattern(regexp = ".*\\#.*\\..*")
I have solved myself this problem, it's way more simple than I have expected it would be.
Validation works simply by adding #Valid annotation above user and customer fields in this wrapper, so that spring knows to actually validate them "deeper".

Resources