Home > OS >  Java Spring Boot Validation Messages not working
Java Spring Boot Validation Messages not working

Time:11-27

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.

CodePudding user response:

If you do not use custom validation message, try to put this the application.properties.

server.error.include-message=always
server.error.include-binding-errors=always

Or, If you'd like to add your custom validation message, add a configurations for message and validator.

@Configuration
public class MessageConfig {
    private static final String MESSAGE_SOURCE_PATH = "classpath:/message";

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename(MESSAGE_SOURCE_PATH);
        messageSource.setDefaultEncoding("UTF-8");

        return messageSource;
    }
}
@Configuration
public class ValidationConfig {
    private final MessageSource validationMessageSource;

    @Bean
    public LocalValidatorFactoryBean getValidator() {
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        bean.setValidationMessageSource(validationMessageSource);

        return bean;
    }
}

Then, adding the messages you want in the message path. message.properties (under resources folder)

validation.title.empty= Please enter the title 
  • Custom validation message usage
@NotBlank(message="{validation.title.empty}")
private String Title;

This is basic setting for the custom validation. Hope you resolve your issue.

  • Related