This is an automated email from the ASF dual-hosted git repository.

reiern70 pushed a commit to branch pattern-validator-reverse-10.x
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit 2a8ff915c4c3d86abcc0c41ed9842cc9f5564f74
Author: reiern70 <[email protected]>
AuthorDate: Thu Nov 12 18:08:16 2026 -0500

    Render no pattern attribute for a reversed PatternValidator
    
    The HTML5Attributes behavior copied the pattern of every PatternValidator
    of a text component 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 renderer, a method added to the API with no
    change to existing members. An application that adds HTML5Attributes to a
    field with a reversed validator sees the field submit again, with the
    server reporting matching input as before. Without HTML5Attributes, or
    for a validator that is not reversed, nothing changes.
    
    GitHub issue #1886
---
 .../wicket/markup/html/form/TextFieldTest.java     | 28 ++++++++++++++++++++++
 .../apache/wicket/markup/html/HTML5Attributes.java |  5 ++--
 .../validation/validator/PatternValidator.java     | 20 +++++++++++++---
 3 files changed, 48 insertions(+), 5 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 de433edc1a..20e0afc13d 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;
 
@@ -102,6 +104,32 @@ class TextFieldTest extends WicketTestCase
                assertEquals("required", required);
        }
        
+       @Test
+       void html5AttributesRendersPatternAttribute()
+       {
+               TestPage testPage = new TestPage();
+               testPage.textField.setOutputMarkupId(true);
+               testPage.textField.add(new PatternValidator("[a-z]+"));
+               testPage.textField.add(new HTML5Attributes());
+               tester.startPage(testPage);
+
+               TagTester tagTester = 
tester.getTagById(testPage.textField.getMarkupId());
+               assertEquals("[a-z]+", 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 5d72686154..f50a1386b2 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
@@ -85,9 +85,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/validation/validator/PatternValidator.java
 
b/wicket-core/src/main/java/org/apache/wicket/validation/validator/PatternValidator.java
index d0dca7a5e5..335cf736e0 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,7 +49,7 @@ import org.apache.wicket.validation.ValidationError;
  * 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.
+ * not rendered, and a {@link #setReverse(boolean) reversed} validator renders 
no attribute at all.
  * 
  * @see java.util.regex.Pattern
  * 
@@ -125,8 +125,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 +138,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()
         */

Reply via email to