Permalink
Browse files

- Added support for multiple contexts

- fixed unique constraint bug
  • Loading branch information...
1 parent 886bdf7 commit 9e449178617eb314df903278ce129f1ab9a48c3a dvega committed May 11, 2012
View
@@ -43,10 +43,10 @@
<p>This framework gives you multiple ways to declare your validation rules or as the framework refers to them, constraints. Once you have defined your constraints you can pass in an object to the framework and it will let you know if it passes validation. This approach gives you a simple way to perform data validation in your object oriented based applications. </p>
<p>This framework was created because there was a real need for it. When I started building most of my applications in a model driven form it became clear that there had to be an easier way of validating data. My inspiration was and still is the <a href="http://www.hibernate.org/subprojects/validator.html" target="_blank">Hibernate Validator</a> project from the Java community. This project also wouldn't be where it is today without the help of some amazing people. In no particular order they are:</p>
<ul>
- <li>Aaron Greenlee</li>
<li>Lance Staples</li>
- <li>Tony Nelson</li>
+ <li>Aaron Greenlee</li>
<li>Luis Majano</li>
+ <li>Tony Nelson</li>
</ul>
<a name="requirements"></a>
@@ -74,9 +74,10 @@
<li><strong>custom:</strong> An example of a custom validator. If the framework is missing a constraint you can create your own.</li>
<li><strong>displayname:</strong> An example of how to use the displayName property to customize property names display value</li>
<li><strong>frameworks:</strong> Full featured examples using the frameworks Coldbox,FW/1 and ColdMVC.</li>
+ <li><strong>Multiple Contexts:</strong> An example that shows that properties can belong to multiple contexts.</li>
<li><strong>property:</strong> An example of declaring constraints at the property level.</li>
<li><strong>rb:</strong> An example of how to use Internationalization and Localization in your application.</li>
- <li><strong>tagbased:</strong> An example that shows declaring constraints in tag based components.</li>
+ <li><strong>tagbased:</strong> An example that shows declaring constraints in tag based components.</li>
</ul>
</li>
<li>
@@ -7,9 +7,6 @@
<cfset result = hyrule.validate(emp)>
-<cfdump var="#result.getErrors()#">
-<cfabort>
-
<!--- if there are errors display them, else save the record --->
<cfif result.hasErrors()>
<cfdump var="#result.getErrors()#">
@@ -79,11 +79,11 @@ component accessors="true" {
if(result.propertyHasError(validationRule.getPropertyName()) && arguments.stopOnFirstFail == 'property') continue;
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())
- && !listFindNoCase(arguments.context,validationRule.getContext())) continue;
+ && !listFindNoCase(validationRule.getContext(),arguments.context)) continue;
var propertyValue = evaluate("arguments.target.get#validationRule.getPropertyName()#()");
var constraint = getConstraintFactory().getConstraint(validationRule.getConstraintName());
@@ -79,4 +79,21 @@ component accessors="true" {
public boolean function passOnNULL(){
return true;
}
+
+ /**
+ * I will return the id of an entity
+ */
+ public any function getFieldTypeId(any entity){
+ var meta = getMetaData(arguments.entity);
+ var id = 0;
+
+ for( var x=1; x <= arrayLen(meta.properties); x++){
+ if( structKeyExists(meta.properties[x],"fieldtype") && meta.properties[x].fieldtype == "id"){
+ id = meta.properties[x].name;
+ break;
+ }
+ }
+
+ return id;
+ }
}
@@ -34,7 +34,7 @@ component accessors="true" {
// read this directory and get a list of constraints
var dir = getDirectoryFromPath(getCurrentTemplatePath());
// for each constraint instantiate the class and set it in the constraints collection
- var components = directoryList(dir);
+ var components = directoryList(dir,false,"name","*.cfc");
// remove exclusions
for(var x=1; x<=arrayLen(components); ++x){
@@ -36,8 +36,18 @@ component extends="AbstractConstraint" accessors="true" {
// is orm enabled && the target class we are looking at a mapped entity
if( (structKeyExists(appSettings,"ormEnabled") && appSettings.ormEnabled) && len(entityName) ){
- var sql = "from " & entityName & " where " & prop & " = ?";
- unique = arrayLen(ormExecuteQuery(sql,[arguments.value])) == 0;
+ // the property with a fieldtype of id
+ var id = getFieldTypeId(arguments.target);
+ var identity = evaluate( "arguments.target.get#id#()" );
+
+ if( isNull( identity ) ){
+ var sql = "from " & entityName & " where " & prop & " = ?";
+ unique = arrayLen(ormExecuteQuery(sql,[arguments.value])) == 0;
+ } else {
+ var sql = "from " & entityName & " where " & prop & " = ? " & " and #id# != ?";
+ unique = arrayLen(ormExecuteQuery(sql,[arguments.value,identity])) == 0;
+ }
+
}
}
}

0 comments on commit 9e44917

Please sign in to comment.