Commits

Anonymous committed 73f970a

RegexFieldValidator fails matching leading and trailing whitespace

Issue Number: XW-459

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1302e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 7008a71

Comments (0)

Files changed (1)

File src/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java

 
     private String expression;
     private boolean caseSensitive = true;
+    private boolean trim = true;
 
     public void validate(Object object) throws ValidationException {
         String fieldName = getFieldName();
             pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
         }
 
-        Matcher matcher = pattern.matcher(str);
+        String compare = (String) value;
+        if ( trim ) {
+            compare = compare.trim();
+        }
+        Matcher matcher = pattern.matcher( compare );
 
         if (!matcher.matches()) {
             addFieldError(fieldName, object);
         this.caseSensitive = caseSensitive;
     }
 
+    /**
+     * @return Returns whether the expression should be trimed before matching.
+     * Default is <code>true</code>.
+     */
+    public boolean isTrimed() {
+        return trim;
+    }
+
+    /**
+     * Sets whether the expression should be trimed before matching.
+     * Default is <code>true</code>.
+     */
+    public void setTrim(boolean trim) {
+        this.trim = trim;
+    }
+
 }