diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/form/FormVisitTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/form/FormVisitTest.java index 4e2f0396249..79600f8f92d 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/form/FormVisitTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/form/FormVisitTest.java @@ -381,6 +381,25 @@ public void callFormOnValidateModelObjectsInPostorder() page.innerForm.onValidateModelObjectsCallOrder < page.outerForm.onValidateModelObjectsCallOrder); } + @Test + public void validationNotPerformedIfAllDependentsDisabled() { + formValidator.setDependency(page.outerField); + page.outerField.setEnabled(false); + page.outerForm.add(formValidator); + tester.newFormTester("outerForm").submit(); + assertFalse(formValidator.validatedCalled, "Validation should not be performed if all dependents are disabled"); + } + + @Test + public void validationPerformedIfAtLeastOneDependentEnabled() { + page.innerField.setEnabled(false); + page.outerField.setEnabled(true); + formValidator.dependencies = new FormComponent[] { page.outerField, page.innerField }; + page.outerForm.add(formValidator); + tester.newFormTester("outerForm").submit(); + assertTrue(formValidator.validatedCalled, "Validation should be performed if at least one dependent is enabled"); + } + public static class TestFormPage extends WebPage implements IMarkupResourceStreamProvider { TestForm outerForm; @@ -535,7 +554,7 @@ public void validate(Form form) validatedCalled = true; } - public FormValidator setDependency(FormComponent component) + public FormValidator setDependency(FormComponent component) { this.dependencies = new FormComponent[] { component }; return this; diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java index 0a9326ad7cd..dfa53458ec2 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java @@ -1962,7 +1962,7 @@ public void validate(final FormComponent formComponent) } /** - * Validates form with the given form validator + * Validates form with the given form validator. * * @param validator */ @@ -1972,10 +1972,35 @@ protected final void validateFormValidator(final IFormValidator validator) final FormComponent[] dependents = validator.getDependentFormComponents(); - boolean validate = true; + boolean validate = false; - if (dependents != null) + if (dependents == null || dependents.length == 0) { + validate = true; + } + else + { + for (final FormComponent dependent : dependents) + { + if (dependent.isEnabledInHierarchy()) + { + validate = true; + break; + } + } + + if (!validate) + { + // no enabled dependents, so no need to validate + if (log.isWarnEnabled()) + { + log.warn("IFormValidator in form `" + + getPageRelativePath() + + "` depends on components, but none have been enabled."); + } + return; + } + for (final FormComponent dependent : dependents) { // check if the dependent component is valid @@ -1986,13 +2011,13 @@ protected final void validateFormValidator(final IFormValidator validator) } // check if the dependent component is visible and is attached to // the page - else if (!dependent.isVisibleInHierarchy() || !dependent.isEnabledInHierarchy() || !dependent.isFormParticipant()) + else if (!dependent.isVisibleInHierarchy() || !dependent.isFormParticipant()) { if (log.isWarnEnabled()) { log.warn("IFormValidator in form `" + getPageRelativePath() + - "` depends on a component that has been removed from the page or is no longer visible/enabled. " + + "` depends on a component that has been removed from the page or is no longer visible. " + "Offending component id `" + dependent.getId() + "`."); } validate = false; diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/validation/IFormValidator.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/validation/IFormValidator.java index 661fa1da583..53c918eece6 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/validation/IFormValidator.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/validation/IFormValidator.java @@ -21,11 +21,11 @@ import org.apache.wicket.util.io.IClusterable; /** - * Interface that represents validators that check multiple components. These validators are added - * to the form and only executed if all form components returned by + * Interface representing validators that check multiple components. These validators are added + * to a form and are executed only if all form components returned by * {@link IFormValidator#getDependentFormComponents()} have been successfully validated before this - * validator runs. - * + * validator is executed. The validator is also executed if the dependent form components + * return null or an empty array. * * @see AbstractFormValidator * @see org.apache.wicket.validation.IValidator @@ -39,8 +39,10 @@ public interface IFormValidator extends IClusterable FormComponent[] getDependentFormComponents(); /** - * This method is ran if all components returned by - * {@link IFormValidator#getDependentFormComponents()} are valid. + * This method is executed if there are no dependent form components, or if all components + * returned by {@link IFormValidator#getDependentFormComponents()} are valid and visible. + * If the dependent form components array is not empty, at least one of the components + * must be enabled. * *

* To report validation error use