Skip to content

Commit e010706

Browse files
committed
* Added the Unique validator thanks to @elpete!
1 parent d04e3e3 commit e010706

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.0.1
44

55
* Updated `RequiredUnless` and `RequiredIf` to use struct literal notation instead of the weird parsing we did.
6+
* Added the `Unique` validator thanks to @elpete!
67

78
## 2.0.0
89

models/validators/AcceptedValidator.cfc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ component accessors="true" singleton {
1818

1919
/**
2020
* Will check if an incoming value validates
21-
* @validationResultThe result object of the validation
22-
* @targetThe target object to validate on
23-
* @fieldThe field on the target object to validate on
24-
* @targetValueThe target value to validate
25-
* @validationDataThe validation data the validator was created with
21+
*
22+
* @validationResult The result object of the validation
23+
* @target The target object to validate on
24+
* @field The field on the target object to validate on
25+
* @targetValue The target value to validate
26+
* @validationData The validation data the validator was created with
2627
*/
2728
boolean function validate(
2829
required any validationResult,
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright since 2020 by Ortus Solutions, Corp
3+
* www.ortussolutions.com
4+
* ---
5+
* This validator checks the database according to validation data for field uniqueness
6+
* - table : The table name to seek
7+
* - column : The column to evaluate for uniqueness or defaults to the name of the field
8+
*/
9+
component accessors="true" singleton {
10+
11+
property name="name";
12+
13+
/**
14+
* Constructor
15+
*/
16+
UniqueValidator function init(){
17+
variables.name = "Unique";
18+
return this;
19+
}
20+
21+
/**
22+
* Will check if an incoming value validates
23+
*
24+
* @validationResult The result object of the validation
25+
* @target The target object to validate on
26+
* @field The field on the target object to validate on
27+
* @targetValue The target value to validate
28+
* @validationData The validation data the validator was created with
29+
*/
30+
boolean function validate(
31+
required any validationResult,
32+
required any target,
33+
required string field,
34+
any targetValue,
35+
any validationData
36+
){
37+
// Default the target column
38+
var targetColumn = ( isNull( arguments.validationData.column ) ? arguments.field : arguments.validationData.column );
39+
// Query it
40+
var exists = queryExecute(
41+
"SELECT 1 FROM #arguments.validationData.table# WHERE #targetColumn# = ?",
42+
[ arguments.targetValue ]
43+
).recordCount > 0;
44+
45+
if ( !exists ) {
46+
return true;
47+
}
48+
49+
validationResult.addError(
50+
validationResult.newError(
51+
argumentCollection = {
52+
message : "The #targetColumn# '#arguments.targetValue#' is already in use",
53+
field : arguments.field,
54+
validationType : getName(),
55+
validationData : arguments.validationData
56+
}
57+
)
58+
);
59+
60+
return false;
61+
}
62+
63+
/**
64+
* Get the name of the validator
65+
*/
66+
string function getName(){
67+
return "Unique";
68+
}
69+
70+
}

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ this.constraints = {
153153

154154
// UDF to use for validation, must return boolean accept the incoming value and target object, validate(value,target):boolean
155155
udf = variables.UDF or this.UDF or a closure.
156+
157+
// Check if a column is unique in the database
158+
unique = {
159+
table : The table name,
160+
column : The column to check, defaults to the property field in check
161+
}
156162

157163
// Custom validator, must implement coldbox.system.validation.validators.IValidator
158164
validator : path or wirebox id, example: 'mypath.MyValidator' or 'id:MyValidator'

0 commit comments

Comments
 (0)