koszek koszek - 6 months ago 40
Ajax Question

Wicket - Update model with ajax without validation

Wicket - Update model with ajax without validation

I have a form with several checkboxes of the layout: "[*] Items of type A (count: 2)".
Each such checkbox has a link attached ("count: 2" in the above layout) that displays the number of items in the list attached to the corresponding checkbox. The link opens another form for adding items to the list.
If the user checks such a checkbox the link becomes enabled and she can add items. But if the checkbox is checked and no items are added an error must be shown when form is submitted.

I tried using AjaxCheckbox to enable/disable the link and it worked fine until i added a validator to the checkbox (to check the above condition).
The problem is: when ajax request is sent from the checkbox the validator is called and validation fails as there are no items in the list yet.

Is there a way in Wicket to have a validator on a component and only use it when submitting the form and not when sending ajax requests? In other words ajax that just updates the model.

I know I can use IFormValidator which is not called in AjaxFormComponentUpdatingBehavior cycle, but then i would probably have to create as many of them as there are checkboxes on the form which i would like to avoid.

PS: Sorry for not providing a picture, my dev machine is in an isolated secured LAN and I can't just copy a file from there.

Answer

You can add an IFormValidator to your form, which will only be called when the whole form is submitted:

public class ItemsValidator extends AbstractFormValidator
{

  @Override
  public FormComponent<?>[] getDependentFormComponents()
  {
    return new FormComponent[]{checkbox};
  }

  @Override
  public void validate(Form<?> form)
  {
    if (checkbox.getConvertedInput() && items.isEmpty())
    {
      error(checkbox);
    }
  }
}
Comments