Permalink
Browse files

Adding method constraint to fire validation method on target object

  • Loading branch information...
1 parent e1b356d commit 684943ecd33451059ffea68bc441b4aeeb59e77a @lstaples lstaples committed Apr 6, 2012
Showing with 106 additions and 0 deletions.
  1. +16 −0 samples/method/index.cfm
  2. +43 −0 samples/method/user.cfc
  3. +46 −0 system/core/constraint/methodConstraint.cfc
  4. +1 −0 system/i18n/default.properties
View
@@ -0,0 +1,16 @@
+<html>
+<head>
+ <title>Advanced example</title>
+</head>
+
+<body>
+
+ <cfset user = new User()>
+ <cfset user.setFirstName("Dan")>
+
+ <cfset result = application.hyrule.validate(user,"firstName")>
+
+ <cfdump var="#result.getErrors()#">
+
+</body>
+</html>
View
@@ -0,0 +1,43 @@
+/**
+ * @output false
+ * @accessors true
+ */
+component {
+
+ property numeric userId;
+
+ /**
+ * @method isNameFullName
+ */
+ property string firstname;
+
+ property string lastname;
+
+ /**
+ * @validator hyrule.samples.custom.userService.isValidUsername
+ */
+ property string username;
+
+ /**
+ * @Email
+ */
+ property string email;
+
+ /**
+ * @Range 1,11
+ * @Matches confirmPassword
+ */
+ property string password;
+
+ /**
+ * @Range 1,11
+ */
+ property string confirmPassword;
+
+ //if we got a first name we need a last and vice versa
+ boolean function isNameFullName(){
+ return ( isNULL(getFirstName()) && isNULL(getLastName()) ) || ( !isNULL(getFirstName()) && !isNULL(getLastName()) );
+ }
+
+
+}
@@ -0,0 +1,46 @@
+/**
+ * methodConstraint
+ *
+ * @hint I am used when you want to call a method on the target to use
+ *
+ */
+component extends="AbstractConstraint" accessors="true" {
+
+ /**
+ * I am the method on the target object to use. I
+ *
+ * this.constraints = {
+ * email = {method="isNameFullName"}
+ * }
+ */
+ property name="method";
+
+ public methodConstraint function init(){
+ setClazz(this);
+ setConstraintName("METHOD");
+ return this;
+ }
+
+ public void function setConstraintParameter(any constraintParameter){
+
+ if( !isValid("string",arguments.constraintParameter)) {
+ throw(
+ type="METHOD_CONSTRAINT.INVALID_CONSTRAINT",
+ message="Paramater for constraint validator was not a string."
+ );
+ }
+
+ setMethod(constraintParameter);
+ }
+
+ public boolean function processValidate(any target, any property, any value){
+ var result = evaluate("arguments.target.#getMethod()#()");
+
+ if( !isValid("boolean",result) ){
+ throw();
+ } else {
+ return result;
+ }
+ }
+
+}
@@ -52,3 +52,4 @@ type.maxlength=
#CUSTOM CONSTRAINTS USE A DIFFERENT FORMAT
custom.userService.isValidUsername=The username you entered already exists in our system, please try again.
product.categories.size=Please select at least one category for this product.
+user.firstName.method=If either first or last name is specified both are required.

0 comments on commit 684943e

Please sign in to comment.