Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Wicket验证器不';t在空字段值上引发错误_Java_Wicket_Validation - Fatal编程技术网

Java Wicket验证器不';t在空字段值上引发错误

Java Wicket验证器不';t在空字段值上引发错误,java,wicket,validation,Java,Wicket,Validation,如何让验证器在空值上引发错误?我可以通过我的日志看到,当我试图提交一个空字段时,它正在调用验证器,但它从未引发onError,所以我无法处理这个问题。当有人试图提交一个空字段时,我希望在反馈面板中显示一条错误消息,而不必在提交我的表单时签入 @Override protected void onValidate(IValidatable<String> validatable) { String value = validatable.getValue(); if

如何让验证器在空值上引发错误?我可以通过我的日志看到,当我试图提交一个空字段时,它正在调用验证器,但它从未引发onError,所以我无法处理这个问题。当有人试图提交一个空字段时,我希望在反馈面板中显示一条错误消息,而不必在提交我的表单时签入

@Override
protected void onValidate(IValidatable<String> validatable) {

    String value = validatable.getValue();

    if (value == null) {
        ValidationError error = new ValidationError();
        error.addMessageKey("messageKey");
        validatable.error(error);
    }

}

@Override
public void validateOnNullValue() {
     return true;
}
以下是我的属性文件中的内容:

upc.null=UPC cannot be null
upc.Required=UPC is required

currentLpn.null=LPN cannot be null
currentLpn.Required=LPN is required

只有在字段中输入信息并验证后,我才会收到*.Required消息,然后将其删除并再次尝试验证。如果我只是加载表单并提交它,而没有输入任何数据,那么我什么也得不到。

如果您只想验证一个字段(如果该字段不是空的),请设置所需字段:

component.setRequired(Boolean.TRUE);
component.setRequired(true);
然后,如果要向用户提供反馈,则必须使用
反馈面板
。将反馈面板配置为在出现任何消息时可见:

private FeedbackPanel feedbackPanel;
feedbackPanel  = new FeedbackPanel("feedbackPanel") {
    @Override
    protected void onConfigure() {
        super.onConfigure(); 
        setVisible(anyMessage());
    }
};
feedbackPanel.setOutputMarkupPlaceholderTag(true);
AjaxButton btn = new AjaxButton("button") {
    @Override
    protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
        super.onSubmit(target, form); 
        target.add(feedbackPanel);
    }
};
然后,如果使用AJAX提交表单,则必须将反馈面板添加到
AjaxRequestTarget
中,以便重新渲染,如果存在任何(错误)消息,反馈面板将显示以下消息:

private FeedbackPanel feedbackPanel;
feedbackPanel  = new FeedbackPanel("feedbackPanel") {
    @Override
    protected void onConfigure() {
        super.onConfigure(); 
        setVisible(anyMessage());
    }
};
feedbackPanel.setOutputMarkupPlaceholderTag(true);
AjaxButton btn = new AjaxButton("button") {
    @Override
    protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
        super.onSubmit(target, form); 
        target.add(feedbackPanel);
    }
};
如果字段为空,则错误消息为:

“用户名”字段不能为空


如果您只想验证一个字段(如果该字段不为空),请设置所需字段:

component.setRequired(Boolean.TRUE);
component.setRequired(true);
然后为错误消息添加标签

component.setLabel(Model.of("Username"));
然后添加一个反馈面板

FeedbackPanel feedback = new FeedbackPanel("feedback");
feedback.setOutputMarkupId(true);
form.add(feedback);
要完成,您必须@Override AjaxSubmitLink中的onError方法:

add(new AjaxSubmitLink("save", form) {
    private static final long serialVersionUID = 1L;
    @Override
    protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
        super.onSubmit(target, form);

    }

    @Override
    protected void onError(AjaxRequestTarget target, Form<?> form) {
        super.onError(target, form);
        target.add(feedback);
    }
});
添加(新的AjaxSubmitLink(“保存”,表单){ 私有静态最终长serialVersionUID=1L; @凌驾 提交时受保护的void(AjaxRequestTarget目标,表单){ super.onSubmit(目标、形式); } @凌驾 受保护的void onError(AjaxRequestTarget目标,表单){ super.onError(目标、形式); 目标。添加(反馈); } }); 如果字段为空,则错误消息为:

“用户名”字段不能为空


为什么不设置字段
setRequired()
?如何在反馈面板中显示错误消息?我的理解是,如果setRequired=true,即使字段为空,也不会执行验证程序。如果字段为空,setRequired会阻止表单提交,但在输入一些数据并删除之前,它仍然不会显示错误消息。此外,您可以将属性文件添加到项目中,使用该文件可以覆盖“null-value”错误。添加一个名为“MyPage.properties”的文件(显然对于一个名为“MyPage”的类),并输入一个键值对:myform.mycontainer.mycomponent.null=“必须提交mycomponent的值!”这很有趣,因为当我加载页面时,如果我只提交表单而不触及任何字段,我不会得到任何错误。如果我在字段中输入了无效值,请提交表单,我将收到预期的验证程序错误消息。如果清除该字段并重新提交表单,则会收到错误消息,这是我在属性文件中定义的空字段所期望的。为什么我在输入数据并清除字段之前提交表单时无法收到此消息?