forked from coldbox-modules/cbwire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationService.cfc
More file actions
68 lines (63 loc) · 2.7 KB
/
ValidationService.cfc
File metadata and controls
68 lines (63 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
component accessors="true" singleton {
property name="validationManager" inject="provider:ValidationManager@cbvalidation";
/**
* Retrieves an instance of the ValidationManager from WireBox.
*
* @return ValidationManager An instance of the cbvalidation ValidationManager.
* @throws CBWIREException If the cbvalidation module is not found.
*/
function getManager() {
try {
return validationManager.$get();
} catch ( any e ) {
throw( type="CBWIREException", message="ValidationManager not found. Make sure the 'cbvalidation' module is installed. #e.message#" );
}
}
/**
* Checks whether the cbvalidation module is installed and accessible.
*
* @return boolean True if the module is available, otherwise false.
*/
function isCBValidationInstalled() {
try {
getManager();
return true;
} catch ( any e ) {
return false;
}
}
/**
* Validates the given data using cbvalidation. Falls back to component data and constraints if not provided.
*
* @wire The CBWIRE component instance.
* @target (optional) The target data to validate. Defaults to the component's data.
* @fields (optional) Fields to validate.
* @constraints (optional) Constraints to apply. Defaults to the component's constraints.
* @locale (optional) Locale to use for validation messages.
* @excludeFields (optional) Fields to exclude from validation.
* @includeFields (optional) Fields to include in validation.
* @profiles (optional) Profiles to use in validation.
*
* @return ValidationResult The result of the validation operation.
*/
function validate( wire, target, fields, constraints, locale, excludeFields, includeFields, profiles ){
if( isNull( arguments.target ) ){
// if no target is provided, default to the wire's data properties
arguments.target = arguments.wire._getDataProperties();
// use the wire's constraints if explicit constraints are not provided
arguments.constraints = isNull( arguments.constraints ) ? arguments.wire._getConstraints() : arguments.constraints
}
return getManager().validate( argumentCollection = arguments );
}
/**
* Validates the component's data and throws an exception if validation fails.
*
* @throws ValidationException If validation fails.
*/
function validateOrFail( required wire ){
local.validationResults = validate( arguments.wire );
if ( local.validationResults.hasErrors() ) {
throw( type="ValidationException", message="Validation failed" );
}
}
}