diff --git a/src/main/java/org/olat/user/propertyhandlers/Generic127CharTextPropertyHandler.java b/src/main/java/org/olat/user/propertyhandlers/Generic127CharTextPropertyHandler.java index d523d070cb8af9a6d093b205153cc7c35cd5df05..5fed3f91e25f70c6e14e40b823525f7053ad6f1b 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; + } }