4
votes

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.

2
I suppose you got it from here? github.com/hibernate/hibernate-validator/blob/master/… Dit it work from original source tree? - Alex Chernyshev
Please add the Imports for the annotations of the entity. - Grim
Hi Peter, could you give more information? - Rocky Hu

2 Answers

3
votes

You are using a Hibernate Validator 4.x version, which is aligned with Bean Validation 1.0. In the 4.x versions variable interpolation using Expression Language (EL) is not available. This is a feature added to Bean Validation 1.1 and hence available in the Hibernate Validator 5.x series. The latest stable version is 5.1.3.Final. I recommend you upgrade to that version.

1
votes

In hibernate 4.3, you need to specify ValueFormatterMessageInterpolator as the message interpolator instead of using the default in order to interpolate ${validatedValue}:

Configuration<?> configuration = Validation.byDefaultProvider().configure();
ValidatorFactory factory = configuration
    .messageInterpolator(new ValueFormatterMessageInterpolator(configuration.getDefaultMessageInterpolator()))
    .buildValidatorFactory();

Validator validator = factory.getValidator();