Issue
I want to show the message thrown by bindingresult.reject("message")
on my web page.
if (product.getQuantity() < saleDetailDto.getQuantity()) {
bindingResult.reject("Not enough item in inventory");
return "purchase-detail/save-form";
}
Solution
You need to determine for which field you reject.
The rejectValue()
method is used to add a validation error to the BindingResult
object.
The first parameter identifies which field the error is associated with.
The second parameter is an error code which acts a message key for the messages.properties
file (or messages_en.properties
or messages_fr.properties
etc., if these are being used).
The third parameter of rejectValue()
represents the fallback default message, which is displayed if no matching error code is found in the resource bundle.
if(product.getQuantity() < saleDetailDto.getQuantity()){
bindingResult.rejectValue("quantity", "error.quantity", "Not enough item in inventory!");
return "purchase-detail/save-form";
}
To show it on html, you can do as follows.
<span th:if="${#fields.hasErrors('quantity')}" th:errors="*{quantity}">Error!</span>
Answered By - İsmail Y.
Answer Checked By - Senaida (JavaFixing Volunteer)