So I can't make these default messages change to the ones I want. In all the JSP files they work great, but I can't make them work inside a form.
Here is my UserController:
@RequestMapping(path = "/user/profile", method = RequestMethod.POST)
public ModelAndView userProfilePost(@ModelAttribute("userForm") @Valid UserForm userForm, BindingResult result) {
if(result.hasErrors()){
return userProfileGet(userForm);
}
User user = us.findByEmail(SecurityContextHolder.getContext().getAuthentication().getName());
user = userHelper.update(userForm, us ,user);
userForm = new UserForm();
userForm.setName(user.getName());
userForm.setEmail(user.getEmail());
return userProfileGet(userForm);
}
Then we have my WebConfig:
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
@Bean
public MessageSource messageSource()
{
final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:languages_i18n/messages", "classpath:languages_i18n/validation");
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.displayName());
messageSource.setCacheSeconds(5);
return messageSource;
}
My UserForm:
public class UserForm {
@Email()
private String email;
@Size(max=100 , min=3)
private String password;
@Size(max=100 , min =3)
private String name;
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
Here I tried also doing (which didn't work of course haha):
@Email(message = "{Email.UserForm.email}")
private String email;
@Size(max=100 , min=3, message = "{Size.UserForm.password}")
private String password;
@Size(max=100 , min =3, message = "{Size.UserForm.name}")
private String name;
Here is my validation_en.properties:
Email.UserForm.email = Your e-mail format is not valid
Size.UserForm.password = Your password must have between {min} and {max} characters
Size.UserForm.name = Your name must have between {min} and {max} characters
I know that when I do it right, the lines in the .properties file should get colored but they are still grey, so clearly I am not reaching them correctly. Any comment will be well received, thank you in advance.
Apparently the solution was that the first letter of the form should be lower case, like this (I do not know why):
Size.userForm.password = Your password must have between {2} and {1} characters
Size.userForm.name = Your name must have between {2} and {1} characters
After that we have to add what Tung Phan said that was also correct because parameters are object array, so {min} and {max} were wrong.
Hope this is useful for anyone in the future.