I write a simple example about Hibernate Validator refer to the official document.
The code snippet of Car.java is below:
@Size(min = 2, max = 14, message = "The license plate '${validatedValue}' must be between {min} and {max} characters long")
private String licensePlate;
@Min(value = 2, message = "There must be at least {value} seat${value > 1 ? 's' : ''}")
private int seatCount;
@DecimalMax(value = "350", message = "The top speed ${formatter.format('%1$.2f', validatedValue)} is higher "
+ "than {value}")
private double topSpeed;
The code snippet of CarTest.java is below:
@Test
public void licensePlateTest()
{
Car car = new Car( null, "A", 1, 400.123456, BigDecimal.valueOf( 200000 ) );
String message = validator.validateProperty( car, "licensePlate" )
.iterator()
.next()
.getMessage();
assertEquals(
"The license plate must be between 2 and 14 characters long",
message
);
}
@Test
public void seatCountTest()
{
Car car = new Car( null, "A", 1, 400.123456, BigDecimal.valueOf( 200000 ) );
String message = validator.validateProperty( car, "seatCount" ).iterator().next().getMessage();
assertEquals( "There must be at least 2 seats", message );
}
The validation message of licensePlate is: "The license plate '${validatedValue}' must be between 2 and 14 characters long."
The validation message of seatCount is: "There must be at least 2 seat${value > 1 ? 's' : ''}."
We can see, the EL expression is not working. the pom.xml about my project is:
<!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
I don't know why it isn't working, anyone could help me? Thanks.