2

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.

3
  • 2
    Show your code in controller. You must push error(s) from Controller to View. Commented Nov 25, 2021 at 4:18
  • Add complete code of method in the controller Commented Nov 25, 2021 at 4:36
  • Added the method in cotroller. Commented Nov 25, 2021 at 5:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The training videos look so simple :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.