Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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一个表单,带有两个具有相同验证的提交按钮_Java_Forms_Validation_Wicket - Fatal编程技术网

Java Wicket一个表单,带有两个具有相同验证的提交按钮

Java Wicket一个表单,带有两个具有相同验证的提交按钮,java,forms,validation,wicket,Java,Forms,Validation,Wicket,我使用wicket 7.x。我有一个带有两个提交按钮的表单。这两个按钮在提交事件上执行不同的操作,但它们具有相同的字段验证。我在其中一种方法中重写了AjaxButton onSubmit以分离不同的行为,但我无法传递到相同的验证方法 button = new AjaxButton("id",this.getRootForm()) { @Override protected void onSubmit(AjaxRequestTarget target, Form<?>

我使用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() {...}
button=newAjaxButton(“id”,this.getRootForm()){
@凌驾
Submit上受保护的void(AjaxRequestTarget目标,表单){…}
}
按钮。setDefaultFormProcessing(false);
@凌驾
受保护的void onValidate(){…}
@凌驾
受保护的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);
this.getRootForm().add(新的IFormValidator()){
@凌驾
公共无效验证(表格){
(表格);
}
@凌驾
public FormComponent[]getDependentFormComponents(){
FormComponent[]c=新的FormComponent[6];
c[0]=nome;
c[1]=电子邮件;
c[2]=cognome;
c[3]=indirizzo;
c[4]=电传;
c[5]=验证码;
返回c;
}
});
受保护的void doValidate(表单){…}
button=新建AjaxButton(“id”,this.getRootForm()){
@凌驾
提交时受保护的void(AjaxRequestTarget目标,表单){
(表格);
如果(!form.hasError()){
...
}否则{
添加(反馈面板);
}
}
}
按钮。setDefaultFormProcessing(false);

您应该实现
IValidator
IFormValidator
并在所有
表单中使用它

更新

public class MyValidatingBehavior extends Behavior implements IValidator {


  @Override
  public void onComponentTag(Component component, ComponentTag tag) {
    super.onComponentTag(component, tag);
    if (component.hasErrorMessage()) {
      tag.append("class", "my-error-style", " ");
    }
  }

  @Override
  public void validate(final IValidatable<String> validatable) {
    final String candidate = validatable.getValue();
    if (!isValid(candidate)) {
        validatable.error(new ValidationError(this));
    }
  }
}
公共类MyValidatingBehavior扩展了IValidator{
@凌驾
public void onComponentTag(组件、组件标记){
super.onComponentTag(组件,标记);
if(component.hasErrorMessage()){
append(“class”,“我的错误样式”,“我的错误样式”);
}
}
@凌驾
公共无效验证(最终可验证){
最终字符串候选项=validatable.getValue();
如果(!isValid(候选者)){
validatable.error(新的ValidationError(this));
}
}
}

您可以将自己的IFormValidator添加到表单并调用代码

创建自己的验证方法

void doValidate(Form<?> form) {
   your validation code here for form.
}

this.getRootForm().add(new IFormValidator() {

    void validate(Form<?> form) {
        doValidate(form);
    }
});

@Override
protected void onValidate() {
   doValidate(this);
}
void doValidate(表单){
请在此处输入表单的验证代码。
}
this.getRootForm().add(新的IFormValidator()){
无效验证(表格){
(表格);
}
});
@凌驾
受保护的void onValidate(){
(这个);
}

没有wicket 1.7,您是说wicket 7.X吗?另外,我建议不要使用
onValidate()
方法(不要覆盖它)。只需将验证器添加到组件和表单中,任何SubmitButton都将在默认情况下触发validaiton,除非您明确告诉按钮不要这样做。请看:我非常确定这样实现它会在每次提交时调用doValidate方法两次。Forms Validator将在Forms validate()方法中自动调用,然后该方法将调用可重写的onValidate方法。已编辑答案中的解决方案。谢谢这样我就可以添加一些代码了?我的意思是,如果发生错误,现在我附加Css类。如何使用此验证方法执行此操作?@MatteOpezGenera如果您希望您的组件在出现错误时添加css类,您可以通过编写
行为
并重写
onComponentTag
方法来实现。在该方法中,只需检查
component.hasErrorMessage()
,并相应地修改标记