This is an automated email from the ASF dual-hosted git repository. reiern70 pushed a commit to branch pattern-validator-reverse-11.x in repository https://gitbox.apache.org/repos/asf/wicket.git
commit 8a976afa6227832ecb59c0ca9e02b6357dbf0f32 Author: reiern70 <[email protected]> AuthorDate: Thu Nov 12 18:08:16 2026 -0500 Render no pattern attribute for a reversed PatternValidator AbstractTextComponent and the HTML5Attributes behavior copied the pattern of every PatternValidator into the HTML pattern attribute, ignoring setReverse(true). A reversed validator rejects input that matches the pattern, the attribute makes the browser reject input that does not, so no non-empty value passed both: the browser blocked the submit of a value the server would accept, and the server rejected the rest. A reversed validator now renders no pattern attribute, and its input is checked on the server only. An HTML pattern cannot reliably express "must not match" for an arbitrary Java pattern. PatternValidator gets a public isReverse() getter for the renderers, a method added to the API with no change to existing members. An application with a reversed validator sees its field submit again, with the server reporting matching input as before. Nothing changes for a validator that is not reversed. GitHub issue #1886 --- .../wicket/markup/html/form/TextFieldTest.java | 39 ++++++++++++++++++++++ .../apache/wicket/markup/html/HTML5Attributes.java | 5 +-- .../markup/html/form/AbstractTextComponent.java | 5 +-- .../validation/validator/PatternValidator.java | 23 ++++++++++--- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/form/TextFieldTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/form/TextFieldTest.java index f27a0e1917..6963236787 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/form/TextFieldTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/form/TextFieldTest.java @@ -17,6 +17,7 @@ package org.apache.wicket.markup.html.form; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import org.apache.wicket.MarkupContainer; @@ -31,6 +32,7 @@ import org.apache.wicket.util.string.Strings; import org.apache.wicket.util.tester.FormTester; import org.apache.wicket.util.tester.TagTester; import org.apache.wicket.util.tester.WicketTestCase; +import org.apache.wicket.validation.validator.PatternValidator; import org.apache.wicket.validation.validator.StringValidator; import org.junit.jupiter.api.Test; @@ -101,6 +103,43 @@ class TextFieldTest extends WicketTestCase assertEquals("required", required); } + @Test + void patternAttribute() + { + TestPage testPage = new TestPage(); + testPage.textField.setOutputMarkupId(true); + testPage.textField.add(new PatternValidator("[a-z]+")); + tester.startPage(testPage); + + TagTester tagTester = tester.getTagById(testPage.textField.getMarkupId()); + assertEquals("[a-z]+", tagTester.getAttribute("pattern")); + } + + @Test + void noPatternAttributeForReversedPatternValidator() + { + TestPage testPage = new TestPage(); + testPage.textField.setOutputMarkupId(true); + testPage.textField.add(new PatternValidator("[a-z]+").setReverse(true)); + tester.startPage(testPage); + + TagTester tagTester = tester.getTagById(testPage.textField.getMarkupId()); + assertNull(tagTester.getAttribute("pattern")); + } + + @Test + void html5AttributesRendersNoPatternAttributeForReversedPatternValidator() + { + TestPage testPage = new TestPage(); + testPage.textField.setOutputMarkupId(true); + testPage.textField.add(new PatternValidator("[a-z]+").setReverse(true)); + testPage.textField.add(new HTML5Attributes()); + tester.startPage(testPage); + + TagTester tagTester = tester.getTagById(testPage.textField.getMarkupId()); + assertNull(tagTester.getAttribute("pattern")); + } + @Test void testSearchType() { diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/HTML5Attributes.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/HTML5Attributes.java index 377c96a75d..a855f4bf72 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/HTML5Attributes.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/HTML5Attributes.java @@ -74,9 +74,10 @@ public class HTML5Attributes extends Behavior validator = ((ValidatorAdapter<?>)validator).getValidator(); } - if (validator instanceof PatternValidator) + if (validator instanceof PatternValidator patternValidator && + !patternValidator.isReverse()) { - tag.put("pattern", ((PatternValidator)validator).getPattern().toString()); + tag.put("pattern", patternValidator.getPattern().toString()); } } } diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractTextComponent.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractTextComponent.java index 11b3b159b2..144086478b 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractTextComponent.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractTextComponent.java @@ -161,9 +161,10 @@ public abstract class AbstractTextComponent<T> extends FormComponent<T> validator = ((ValidatorAdapter<?>)validator).getValidator(); } - if (validator instanceof PatternValidator) + if (validator instanceof PatternValidator patternValidator && + !patternValidator.isReverse()) { - tag.put("pattern", ((PatternValidator)validator).getPattern().toString()); + tag.put("pattern", patternValidator.getPattern().toString()); } } diff --git a/wicket-core/src/main/java/org/apache/wicket/validation/validator/PatternValidator.java b/wicket-core/src/main/java/org/apache/wicket/validation/validator/PatternValidator.java index 90e317cdad..bab0ae6a3b 100644 --- a/wicket-core/src/main/java/org/apache/wicket/validation/validator/PatternValidator.java +++ b/wicket-core/src/main/java/org/apache/wicket/validation/validator/PatternValidator.java @@ -49,8 +49,9 @@ import org.apache.wicket.validation.ValidationError; * the input too. The browser compiles that attribute as a JavaScript regular expression with the * <code>v</code> flag and ignores it when it does not compile, so the pattern has to be valid there * as well: escape a <code>-</code> in a character class (<code>[a-z\-]</code>), for example. - * Compile flags such as {@link Pattern#CASE_INSENSITIVE} are not rendered. - * + * Compile flags such as {@link Pattern#CASE_INSENSITIVE} are not rendered, and a + * {@link #setReverse(boolean) reversed} validator renders no attribute at all. + * * @see java.util.regex.Pattern * * @author Jonathan Locke @@ -125,8 +126,10 @@ public class PatternValidator implements IValidator<String> } /** - * If set to true then input that matches the pattern is considered invalid. - * + * If set to true then input that matches the pattern is considered invalid. The browser cannot + * check that, so a reversed validator renders no HTML <code>pattern</code> attribute and the + * input is checked on the server only. + * * @param reverse * @return itself */ @@ -136,6 +139,18 @@ public class PatternValidator implements IValidator<String> return this; } + /** + * Tells whether input that matches the pattern is considered invalid. + * + * @return <code>true</code> if input that matches the pattern is invalid + * @see #setReverse(boolean) + * @since 10.13.0 + */ + public final boolean isReverse() + { + return reverse; + } + /** * @see java.lang.Object#toString() */
