【发布时间】:2017-11-14 10:27:32
【问题描述】:
我使用 wicket 7.x。我有一个带有两个提交按钮的表单。这两个按钮在提交事件时执行不同的操作,但它们具有相同的字段验证。我在其中一个中覆盖了 AjaxButton onSubmit 以分离不同的行为,但我无法传递给相同的验证方法。
button = new AjaxButton("id",this.getRootForm()) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form){...}
}
button.setDefaultFormProcessing(false);
@Override
protected void onValidate() {...}
@Override
protected void onSubmit() {...}
我怎样才能通过所有表单的相同验证方法?
已编辑答案
this.getRootForm().add(new IFormValidator() {
@Override
public void validate(Form<?> form) {
doValidate(form);
}
@Override
public FormComponent<?>[] getDependentFormComponents() {
FormComponent<?>[] c = new FormComponent<?>[6];
c[0] = nome;
c[1] = email;
c[2] = cognome;
c[3] = indirizzo;
c[4] = telefono;
c[5] = captcha;
return c;
}
});
protected void doValidate(Form<?> form) {...}
button = new AjaxButton("id",this.getRootForm()) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form){
doValidate(form);
if (!form.hasError()) {
...
} else{
target.add(feedbackPanel);
}
}
}
button.setDefaultFormProcessing(false);
【问题讨论】:
-
没有wicket 1.7,你的意思是wicket 7.X吗?另外我建议不要使用
onValidate()方法(不要覆盖它)。只需将验证器添加到您的组件和表单中,任何 SubmitButton 都会默认触发该验证,除非您明确告诉按钮不要这样做。见:ci.apache.org/projects/wicket/guide/6.x/guide/…
标签: java forms validation wicket