开发者

Using custom javax validation annotations in GWT

开发者 https://www.devze.com 2023-04-11 10:46 出处:网络
For the current project we\'re using JSR-303 annotations to validate our interface parameters. I needed to create a custom annotation for dates,开发者_StackOverflow中文版 as the default @Past and @Be

For the current project we're using JSR-303 annotations to validate our interface parameters.

I needed to create a custom annotation for dates,开发者_StackOverflow中文版 as the default @Past and @Before also take into account the time.

This is the definition of my annotation:

@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { NotInFutureValidator.class })
/**
 * Annotated element must be a date before tomorrow, compared to the system's date
 */
public @interface NotInFuture {
    String message() default "{be.credoc.contractRegistration.interface_v2.validation.pastignoretime}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

The implementation of the validator is pretty straight forward.

Our webapplication is written in GWT and it makes use of gwt-validation. This allows us to validate our parameters on client side by means of the same annotations.

However, when I annotate my parameters with my custom annotation and I want to validate it by making use of the GwtValidatorFactory, it doesn't give me an error when the input date is in the future.

Does anyone have experiencing defining and using their own annotations in a GWT application and can see what I'm missing?

Thanks in advance


Instead of creating new annotations you might try to re-define the validators for the existing @Past and @Future annotations by providing an XML constraint mapping like this:

<?xml version="1.0" ?>
<constraint-mappings
    xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=
        "http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd">

    <constraint-definition annotation="javax.validation.constraints.Past">
        <validated-by include-existing-validators="false">
            <value>x.y.z.NotInFutureValidator</value>
        </validated-by>
    </constraint-definition>
</constraint-mappings>

This mapping must be registered in the configuration file META-INF/validation.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<validation-config
    xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">

    <constraint-mapping>META-INF/validation/custom-constraints.xml</constraint-mapping>

</validation-config>

You can learn more about custom validators for existing constraints in the Bean Validation specification and the Hibernate Validator reference guide.


Apparently we wrote our own JavaScript implementations for each custom annotation, so the solution in this case was to write such an implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消