You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This module is a server side rules validation engine that can provide you with a unified approach to object, struct and form validation. You can construct validation constraint rules and then tell the engine to validate them accordingly. You can validate objects or structures and even use profiles to target specific fields for validation. The validation engine is highly extensible and can be used in any kind of application.
19
19
20
+
The module leverages the power of the cbi18n module for internationalization support, providing localized validation messages, and integrates seamlessly with ColdBox's WireBox dependency injection container for maximum flexibility and testability.
21
+
20
22
## License
21
23
22
24
Apache License, Version 2.0.
@@ -29,18 +31,27 @@ Apache License, Version 2.0.
29
31
30
32
## Requirements
31
33
32
-
- BoxLang 1+
33
-
- Lucee 5.x+
34
-
- Adobe ColdFusion 2021+
34
+
-**BoxLang** 1.0+ (Preferred)
35
+
-**Lucee** 5.x+
36
+
-**Adobe ColdFusion** 2021+
37
+
-**Dependencies**: ColdBox 7+, cbi18n 3.0+
35
38
36
39
## Installation
37
40
38
41
Leverage CommandBox to install:
39
42
40
-
`box install cbvalidation`
43
+
```bash
44
+
box install cbvalidation
45
+
```
41
46
42
47
The module will register several objects into WireBox using the `@cbvalidation` namespace. The validation manager is registered as `ValidationManager@cbvalidation`. It will also register several helper methods that can be used throughout the ColdBox application: `validate(), validateOrFail(), getValidationManager()`
43
48
49
+
### WireBox Registrations
50
+
51
+
-`ValidationManager@cbvalidation` - The core validation engine
52
+
-`validationManager@cbvalidation` - Alias for convenience
53
+
- Global validation helper methods injected into all ColdBox components
54
+
44
55
## Mixins
45
56
46
57
The module will also register several methods in your handlers/interceptors/layouts/views
@@ -115,14 +126,22 @@ Here are the module settings you can place in your `ColdBox.cfc` by using the `c
115
126
116
127
```js
117
128
modulesettings = {
118
-
cbValidation= {
129
+
cbvalidation= {
119
130
// The third-party validation manager to use, by default it uses CBValidation.
120
131
manager ="class path",
121
132
122
133
// You can store global constraint rules here with unique names
123
134
sharedConstraints = {
124
-
name = {
125
-
field = { constraints here }
135
+
userRegistration = {
136
+
email = { required=true, type="email" },
137
+
password = { required=true, size="8..50" },
138
+
firstName = { required=true, size="1..50" },
139
+
lastName = { required=true, size="1..50" }
140
+
},
141
+
userUpdate = {
142
+
email = { required=true, type="email" },
143
+
firstName = { required=true, size="1..50" },
144
+
lastName = { required=true, size="1..50" }
126
145
}
127
146
}
128
147
}
@@ -229,6 +248,80 @@ this.constraints = {
229
248
}
230
249
```
231
250
251
+
## Usage Examples
252
+
253
+
### Basic Validation in Handlers
254
+
255
+
```js
256
+
functionsaveUser( event, rc, prc ){
257
+
var results =validate( target=rc, constraints="userRegistration" );
258
+
if( results.hasErrors() ){
259
+
prc.errors=results.getAllErrors();
260
+
returnevent.setView( "users/registration" );
261
+
}
262
+
// Process valid data
263
+
var user =userService.create( rc );
264
+
}
265
+
```
266
+
267
+
### Exception Based Validation
268
+
269
+
```js
270
+
functionapiCreateUser( event, rc, prc ){
271
+
try{
272
+
var validData =validateOrFail( target=rc, profiles="registration" );
boxlang-cli test run --runner="http://localhost:60299/tests/runner.cfm"
317
+
318
+
# Run specific test bundle
319
+
boxlang-cli test run --bundles="test-harness/tests"
320
+
321
+
# Run with verbose output
322
+
boxlang-cli test run --verbose
323
+
```
324
+
232
325
## Constraint Profiles
233
326
234
327
You can also create profiles or selections of fields that will be targeted for validation if you are defining the constraints in objects. All you do is create a key called: `this.constraintProfiles` which contains a struct of defined fields:
0 commit comments