Wicket DateTextField 自定义错误消息

Wicket DateTextField custom error message

我正在使用 DateTextField 编辑日期字段, 如果用户输入无效日期(例如,输入日期后的任何字符) 然后在提交表单 DateTextField 内部根据 id 创建无效日期消息。

我的代码是:

form.add(DateTextField.forDatePattern("orderStartDate", new DateModel(new PropertyModel<Date>(this, "defaultModelObject.startDate")), "dd/MM/yyyy"));

现在,如果用户输入任何无效日期,然后在表单提交中,DateTextField 会生成消息。

"The value of 'orderStartDate' is not a valid Date."

在消息中 'orderStartDate' 是我在 DateTextField 上设置的 id;

现在我想为上面消息中的这个错误创建自定义消息;

默认值来自IConverter资源包键:https://github.com/apache/wicket/blob/77b4df63f44d00e9350068686e4b091f730f167f/wicket-core/src/main/java/org/apache/wicket/Application.properties#L16

您需要在 YourApplication.properties 中提供更具体的一个来覆盖它,例如:

orderStartDate.IConverter=Please provide a date with pattern dd/MM/yyyy.

验证器通常将它们的 class 报告到错误对象中。当 Wicket 生成 FeedbackMessage 时,它​​会在 StringResourceModel 中查找它。所以如果你使用.properties或者.properties.xml文件来写字,你可能会想要使用IConverter(报错)的key添加FeedbackMessage

<entry key="IConverter.Date">Please use the format dd/MM/yyyy!</entry>

所需的 FormComponents 同样如此

<entry key="Required">Please provide a value!</entry>

如果有一天你编写自己的验证器,给它一个键和可选参数。

private void error(IValidatable<String> validatable) {
    ValidationError error = new ValidationError();
    error.addKey("id.exists");
    error.setVariable("id", valueThatIsNotValid); // variable held in class
    ekpCmp.error(error);
}

并定义 StringResource

    <entry key="id.exists">The value ${id} already exists.</entry>