Permalink
Browse files

Adding support for context grouping so multiple properties can be tar…

…geted for validation with a single context. Previous property by property method is still supported and the 2 can be mixed and matched
  • Loading branch information...
lstaples committed Apr 6, 2012
1 parent 684943e commit 8d3a1fe98557f424ab5e5d8d9a914d8930a9fa03
View
@@ -0,0 +1,45 @@
+/**
+ * @output false
+ * @accessors true
+ */
+component {
+
+ property numeric userId;
+
+ /**
+ * @Required
+ *@context userSetup
+ */
+ property string firstname;
+
+ /**
+ * @min 3
+ *@context userSetup
+ */
+ property string lastname;
+
+ /**
+ * @max 3
+ *@context userSetup
+ */
+ property string username;
+
+ /**
+ * @Email
+ */
+ property string email;
+
+ /**
+ * @Range 1,11
+ *@context passwordChange
+ */
+ property string password;
+
+ /**
+ * @Range 1,11
+ *@context passwordChange
+ */
+ property string confirmPassword;
+
+
+}
@@ -0,0 +1,19 @@
+<html>
+<head>
+ <title>Advanced example</title>
+</head>
+
+<body>
+
+ <cfset user = new User()>
+ <cfset user.setPassword("myreallylongpassword")>
+ <cfset user.setConfirmPassword("abc")>
+
+ <cfset result = application.hyrule.validate(user,"passwordChange")>
+ <cfdump var="#result.getErrors()#">
+
+ <cfset result = application.hyrule.validate(user,"userSetup,passwordChange")>
+ <cfdump var="#result.getErrors()#">
+
+</body>
+</html>
View
@@ -72,7 +72,9 @@ component accessors="true" {
var type = targetName & "." & validationRule.getPropertyName() & "." & validationRule.getConstraintName();
// if a context is requested and we do not find the property name in the context then skip this contstraint
- if( arguments.context != "*" && !listFindNoCase(arguments.context,validationRule.getPropertyName()) ) continue;
+ if( arguments.context != "*"
+ && !listFindNoCase(arguments.context,validationRule.getPropertyName())
+ && !listFindNoCase(arguments.context,validationRule.getContext()) ) continue;
var propertyValue = evaluate("arguments.target.get#validationRule.getPropertyName()#()");
propertyValue = isNULL(propertyValue)? '' : propertyValue;
@@ -2,6 +2,7 @@
property string propertyName;
property string constraintName;
property string constraintValue;
+ property string context;
ValidationRule function init(){
@@ -43,6 +43,8 @@ component accessors="true" extends="BaseRuleParser" implements="IRuleParser" {
vr.setConstraintName(attributeName);
vr.setConstraintValue(property[attributeName]);
vr.setPropertyName(propertyName);
+ if(structKeyExists(property,"context"))
+ vr.setContext(property.context);
ruleSet.addValidationRule(vr);
}
}
@@ -70,10 +72,12 @@ component accessors="true" extends="BaseRuleParser" implements="IRuleParser" {
for (propertyConstraintName in propertyConstraints){
//if the constraint name isn't recognized skip over it instead of erroring our
if(isValidConstraintName(propertyConstraintName)){
- var validationRule = new hyrule.system.core.rule.ValidationRule();
- validationRule.setConstraintName(propertyConstraintName);
- validationRule.setConstraintValue(propertyConstraints[propertyConstraintName]);
- validationRule.setPropertyName(propertyName);
+ var vr = new hyrule.system.core.rule.ValidationRule();
+ vr.setConstraintName(propertyConstraintName);
+ vr.setConstraintValue(propertyConstraints[propertyConstraintName]);
+ vr.setPropertyName(propertyName);
+ if(structKeyExists(propertyConstraints,"context"))
+ vr.setContext(propertyConstraints.context);
ruleSet.addValidationRule(validationRule);
}
}

0 comments on commit 8d3a1fe

Please sign in to comment.