I am trying to learn Spring Boot.
Control Object partial code:
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
public class WatchlistItem {
@NotBlank(message="Please enter the title")
private String Title;
private String Rating;
private String Priority;
@Size(max=50, message="Comment should be maximum of 50 characters")
private String Comment;
private Integer ID;
Controller partial code:
@PostMapping("/watchlistItemForm")
public ModelAndView getWatchlistItem(@Valid WatchlistItem watchlistItem, BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
return new ModelAndView("watchlistItemForm");
}
WatchlistItem exisiting = findWatchlistItem(watchlistItem.getID());
if (exisiting == null) {
watchlistItem.setID(index++);
watchlistItems.add(watchlistItem);
}else {
//if it is existing then it changes the attributes.
}
RedirectView view = new RedirectView();
view.setUrl("/watchlist");
return new ModelAndView(view);
Html partial Code:
<div class = "col-sm-4">
<input th:field="*{title}" type = "text" class = "form-control" placeholder = "Mandatory">
</div>
<div class="col-sm-4">
<span class="text-danger" th:errors="*{title}"> </span>
</div>
</div>
The form works fine, the form does not accept incomplete information. But the error messages are not displayed. Please advice.
If you do not use custom validation message, try to put this the application.properties.
Or, If you'd like to add your custom validation message, add a configurations for message and validator.
Then, adding the messages you want in the message path. message.properties (under
resourcesfolder)This is basic setting for the custom validation. Hope you resolve your issue.