【问题标题】:Hibernate Validator And Joda DateTime ValidatorHibernate 验证器和 Joda DateTime 验证器
【发布时间】:2015-02-03 03:43:29
【问题描述】:

我有一个带有 Joda DateTime 字段和以下验证规则的 User 模型类:

@NotEmpty
@DateTimeFormat(pattern="dd/MM/yyyy")
@Past
private DateTime dob;

提交注册表后,我收到此错误:

No validator could be found for type: org.joda.time.DateTime.

如何向该字段添加验证器?

【问题讨论】:

  • 愚蠢的问题,但你有“import org.joda.time.format.DateTimeFormat;”在那个班级的某个地方?
  • 我正在使用 org.springframework.format.annotation.DateTimeFormat
  • 换我列出的那个
  • org.joda.time.format.DateTimeFormat 不是注释
  • 是的,你是对的,我意识到我看错了。让我再试一次。你如何进行验证?你能展示你的方法和验证的代码吗?

标签: spring hibernate jodatime thymeleaf


【解决方案1】:

使用@NotNull 而不是@NotEmpty 来解决问题。

【讨论】:

    【解决方案2】:

    您必须编写自己的验证器才能与 Joda 的 DateTime 一起使用。

    @Past 仅适用于(非 Joda)DateCalendar。我不知道有一个库为 Joda 类型提供 Java bean 验证器。

    仅供参考,@Past 的 Javadoc 说明了它支持哪些类型:

    /**
     * The annotated element must be a date in the past.
     * Now is defined as the current time according to the virtual machine.
     * <p/>
     * The calendar used if the compared type is of type {@code Calendar}
     * is the calendar based on the current timezone and the current locale.
     * <p/>
     * Supported types are:
     * <ul>
     *     <li>{@code java.util.Date}</li>
     *     <li>{@code java.util.Calendar}</li>
     * </ul>
     * <p/>
     * {@code null} elements are considered valid.
     *
     * @author Emmanuel Bernard
     */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多