Skip to content

WICKET-7159 - Remove isEnabledInHierarchy condition from validate Form #1186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
* <p>
* To report validation error use
Expand Down