在 GWT 中使用自定义 javax 验证注释

发布于 2024-12-08 18:52:04 字数 873 浏览 0 评论 0原文

对于当前项目,我们使用 JSR-303 注释来验证我们的接口参数。

我需要为日期创建自定义注释,因为默认的 @Past 和 @Before 也考虑了时间。

这是我的注释的定义:

@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 {};
}

验证器的实现非常简单。

我们的 Web 应用程序是用 GWT 编写的,并且使用了 gwt-validation。这允许我们通过相同的注释在客户端验证我们的参数。

但是,当我使用自定义注释来注释我的参数并且我想通过使用 GwtValidatorFactory 来验证它时,当输入日期是将来的日期时,它不会给我错误。

有没有人有在 GWT 应用程序中定义和使用自己的注释的经验,并且可以看到我缺少的内容?

提前致谢

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

I needed to create a custom annotation for dates, 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

递刀给你 2024-12-15 18:52:04

您可以尝试通过提供如下所示的 XML 约束映射来重新定义现有 @Past@Future 注释的验证器,而不是创建新注释:

<?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>

此映射必须是在配置文件 META-INF/validation.xml 中注册,如下所示:

<?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>

您可以在 Bean Validation 规范 和 Hibernate 验证器 参考指南

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.

清欢 2024-12-15 18:52:04

显然,我们为每个自定义注释编写了自己的 JavaScript 实现,因此本例中的解决方案是编写这样的实现。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文