Reputation: 4267
I'm studying Spring 3 and im trying to validate a form using the class org.hibernate.validator.constraints.NotEmpty
.
Im able, for instance, to override the @Size
message (javax.validation.constraints.Size
) and the @Email
message (org.hibernate.validator.constraints.Email
) retrieving the messages from a .properties file.
But im not able to override the org.hibernate.validator.constraints.NotEmpty
default message. I always get the default message "may not be empty"
this is my XXXX-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
<context:component-scan base-package="com.springgestioneerrori.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" >
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
</beans>
This is my User class
package com.springgestioneerrori.model;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class Utente {
@NotEmpty
@Size(min=3,max=20)
private String nome;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
This is my form
.......
<form:form action="formSent" method="post" commandName="utente">
<form:errors path="*" cssClass="errorblock" element="div" /><br/><br/>
<spring:message code="form.label.utente.nome"/><form:input path="nome"/><form:errors path="nome" cssClass="error" /><br>
<input type="submit">
</form:form>
......
this is my controlller
........
@RequestMapping("/formSent")
public String formSent(Model model, @Valid Utente utente, BindingResult result){
if(result.hasErrors()){
model.addAttribute("utente", utente);
return "form";
}
else{
model.addAttribute("msg", "inserimento effettuato con successo");
return "formSent";
}
}
.........
this is my propertis file
NotEmpy.utente.nome = il campo nome non può essere vuoto //Im NOT able to get this message
Size.utente.nome = Il nome deve contenere tra 2 e 30 lettere //Im able to get this message
Upvotes: 1
Views: 10659
Reputation: 4267
Thank for your help. I made a very stupid error. Inside the properties file i wrote NotEmpy instead of NotEmpty. Sorry for waisting you time :)
Upvotes: 0
Reputation: 433
Try this -
@NotEmpty( message = "{NotEmpy.utente.nome}" ) private String nome;
Upvotes: 0
Reputation: 1337
You have to create a file ValidationMessages.properties
and place it in the classpath of your application. The Hibernate validator use the org.hibernate.validator.constraints.NotEmpty.message
key to translate the message
org.hibernate.validator.constraints.NotEmpty.message=il campo nome non può essere vuoto
Have a look here for further information:
UPDATE
For customizing a single message use something like this:
@NotEmpty( message = "{NotEmpy.utente.nome}" )
@Size(min=3,max=20, message = "{Size.utente.nome}")
private String nome;
Upvotes: 3