Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed #1365 New validator: UniqueValidator so you can test for unique…
…ness on ORM enabled object field
- Loading branch information
Showing
5 changed files
with
155 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
******************************************************************************** | ||
Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp | ||
www.coldbox.org | www.luismajano.com | www.ortussolutions.com | ||
******************************************************************************** | ||
Validates if the field has a unique value in the database, this only applies to ORM objects | ||
*/ | ||
component accessors="true" implements="coldbox.system.validation.validators.IValidator" singleton{ | ||
|
||
property name="name"; | ||
property name="ORMService"; | ||
|
||
UniqueValidator function init(){ | ||
name = "Unique"; | ||
ORMService = new coldbox.system.orm.hibernate.BaseORMService(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Will check if an incoming value validates | ||
* @validationResult.hint The result object of the validation | ||
* @target.hint The target object to validate on | ||
* @field.hint The field on the target object to validate on | ||
* @targetValue.hint The target value to validate | ||
* @validationData.hint The validation data the validator was created with | ||
*/ | ||
boolean function validate(required coldbox.system.validation.result.IValidationResult validationResult, required any target, required string field, any targetValue, string validationData){ | ||
|
||
// return true if not unique, nothing needed to check | ||
if( !arguments.validationData ){ return true; } | ||
|
||
// null checks | ||
if( isNull( arguments.targetValue ) ){ | ||
var args = {message="The '#arguments.field#' value is null",field=arguments.field,validationType=getName(),validationData=arguments.validationData}; | ||
validationResult.addError( validationResult.newError(argumentCollection=args) ); | ||
return false; | ||
} | ||
|
||
// Only validate simple values and if they have length, else ignore. | ||
if( isSimpleValue( arguments.targetValue ) AND len( trim( arguments.targetValue ) ) ){ | ||
// process entity setups. | ||
var entityName = ORMService.getEntityGivenName( arguments.target ); | ||
var identityField = ORMService.getKey( entityName ); | ||
var identityValue = evaluate( "arguments.target.get#identityField#()" ); | ||
|
||
// create criteria for uniqueness | ||
var c = ORMService.newCriteria( entityName ) | ||
.isEq( field, arguments.targetValue ); | ||
|
||
// validate with ID? then add to restrictions | ||
if( !isNull( identityValue ) ){ | ||
c.ne( identityField, identityValue ); | ||
} | ||
|
||
// validate uniqueness | ||
if( c.count() GT 0 ){ | ||
var args = {message="The '#arguments.field#' value is not unique in the database",field=arguments.field,validationType=getName(),validationData=arguments.validationData}; | ||
validationResult.addError( validationResult.newError(argumentCollection=args) ); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Get the name of the validator | ||
*/ | ||
string function getName(){ | ||
return name; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
testing/cases/validation/validators/UniqueValidatorTest.cfc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
******************************************************************************** | ||
Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp | ||
www.coldbox.org | www.luismajano.com | www.ortussolutions.com | ||
******************************************************************************** | ||
*/ | ||
component extends="coldbox.system.testing.BaseModelTest" model="coldbox.system.validation.validators.UniqueValidator"{ | ||
|
||
function setup(){ | ||
super.setup(); | ||
model.init(); | ||
} | ||
|
||
function testValidate(){ | ||
result = getMockBox().createMock("coldbox.system.validation.result.ValidationResult").init(); | ||
var category = entityNew("Category"); | ||
|
||
// null | ||
r = model.validate(result, category, 'category', javacast("null",""), "true"); | ||
assertEquals( false, r ); | ||
|
||
// 1: No ID, Unique | ||
r = model.validate(result, category, 'category', "luis", "true"); | ||
assertEquals( true, r ); | ||
// 2: No ID, Not Unique | ||
r = model.validate(result, category, 'category', "ORM", "true"); | ||
assertEquals( false, r ); | ||
|
||
var category = entityLoad("Category", {category="ORM"} , true); | ||
// 3: With ID, the same | ||
r = model.validate(result, category, 'category', "ORM", "true"); | ||
assertEquals( true, r ); | ||
// 3: With ID, and unique | ||
r = model.validate(result, category, 'category', "THIS IS UNIQUE", "true"); | ||
assertEquals( true, r ); | ||
// 4: With ID, and NOT unique | ||
r = model.validate(result, category, 'category', "News", "true"); | ||
assertEquals( false, r ); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.