【问题标题】:Wicket one form with two submit buttons with the same validationWicket 一个带有两个提交按钮的表单,具有相同的验证
【发布时间】: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


【解决方案1】:

您可以添加自己的 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);
}

【讨论】:

  • 我很确定像这样实现它会在每次提交时调用 doValidate 方法两次。 IFormValidators 将在 Forms validate() 方法中自动调用,然后将调用可覆盖的 onValidate 方法。
  • 我用这个方法解决。已编辑答案中的解决方案。谢谢
【解决方案2】:

您应该实现IValidatorIFormValidator 并在您的所有Forms 中使用它。

https://ci.apache.org/projects/wicket/guide/8.x/single.html#_form_validation_and_feedback_messages

更新

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));
    }
  }
}

【讨论】:

  • 这样做我可以添加一些代码吗?我的意思是,现在如果发生错误,我会附加 Css 类。如何使用这种验证方法做到这一点?
  • @MatteoPezzanera 如果您希望您的组件在出现错误时添加一个 css 类,您可以通过编写 Behaviour 并覆盖 onComponentTag 方法来实现它。在该方法中,只需检查 component.hasErrorMessage() 并相应地修改标签
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 2014-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多