From 72a45cac1f3f4b814137641f2ec7327862e2c40a Mon Sep 17 00:00:00 2001
From: Matthai Kurian <matthai.kurian@frentix.com>
Date: Wed, 25 Jul 2012 09:58:25 +0200
Subject: [PATCH] OO-318: added spring configurable regexp and errorkey for
 generic UserProperty

---
 .../Generic127CharTextPropertyHandler.java    | 25 +++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/olat/user/propertyhandlers/Generic127CharTextPropertyHandler.java b/src/main/java/org/olat/user/propertyhandlers/Generic127CharTextPropertyHandler.java
index d523d070cb8..5fed3f91e25 100644
--- a/src/main/java/org/olat/user/propertyhandlers/Generic127CharTextPropertyHandler.java
+++ b/src/main/java/org/olat/user/propertyhandlers/Generic127CharTextPropertyHandler.java
@@ -29,7 +29,7 @@ import org.olat.core.gui.components.form.flexible.FormUIFactory;
 import org.olat.core.id.User;
 import org.olat.user.AbstractUserPropertyHandler;
 import org.olat.user.UserManager;
-
+import java.util.regex.Pattern;
 /**
  * <h3>Description:</h3>
  * This generic text property provides a userfield that has a maximum of 127
@@ -45,6 +45,8 @@ import org.olat.user.UserManager;
  */
 public class Generic127CharTextPropertyHandler extends AbstractUserPropertyHandler {
 	
+	private Pattern regExp;
+	private String regExpMsgKey;
 
 	/**
 	 * @see org.olat.user.propertyhandlers.UserPropertyHandler#updateUserFromFormItem(org.olat.core.id.User, org.olat.core.gui.components.form.flexible.FormItem)
@@ -96,6 +98,11 @@ public class Generic127CharTextPropertyHandler extends AbstractUserPropertyHandl
 		if (textElemItem.isMandatory()) {
 			return ! textElemItem.isEmpty("new.form.mandatory");
 		} 
+
+		if (regExp != null) {
+			return regExp.matcher(textElemItem.getValue()).matches();
+		}
+		
 		return true;
 	}
 
@@ -108,8 +115,22 @@ public class Generic127CharTextPropertyHandler extends AbstractUserPropertyHandl
 			validationError.setErrorKey("general.error.max.127");
 			return false;
 		}
+		
+		if (regExp != null) {
+			if (!regExp.matcher(value).matches()) {
+				validationError.setErrorKey(regExpMsgKey);
+				return false;
+			}
+		}
+		
 		return true;
 	}
-
 	
+	public void setRegExp(String rx) {
+		this.regExp = Pattern.compile(rx);
+	}
+	
+	public void setRegExpMsgKey(String key) {
+		this.regExpMsgKey = key;
+	}
 }
-- 
GitLab