cbValidation
v4.x
v4.x
  • Introduction
  • Intro
    • Release History
      • What's New With 4.4.0
      • What's New With 4.3.1
      • What's New With 4.3.0
      • What's New With 4.2.0
      • What's New With 4.1.0
      • What's New With 4.0.0
    • About This Book
      • Author
  • Overview
    • Installation
    • Configuration
    • Declaring Constraints
      • Configuration File
      • Domain Object
      • A-la-carte
    • Available Constraints
      • Custom Message Replacements
      • Constraint Custom Messages
      • Nested Struct and Array Field Name Shortcuts
    • Validating Constraints
      • Validating With Failures
      • Validating with shared constraints
      • Validating with a-la-carte constraints
      • Validating Custom Fields
      • Validating With Profiles
    • Displaying Errors
    • WireBox Integration
  • Advanced
    • Custom Validators
    • Unique ORM Validator
    • i18n Integration
    • Custom Validation Managers
Powered by GitBook
On this page
  • Declaration
  • Usage

Was this helpful?

Edit on GitHub
Export as PDF
  1. Overview
  2. Declaring Constraints

Domain Object

Within any domain object you can define a public variable called this.constraints that is a assigned an implicit structure of validation rules for any fields or properties in your object.

Declaration

models/User.cfc
component persistent="true"{

    // Object properties
    property name="id" fieldtype="id" generator="native" setter="false";
    property name="fname";
    property name="lname";
    property name="email";
    property name="username";
    property name="password";
    property name="age";

    // Validation
    this.constraints = {
        // Constraints go here
    }
}

We can then create the validation rules for the properties it will apply to it:

config/User.cfc
component persistent="true"{


    ...

    // Validation
    this.constraints = {
        fname = { required = true },
        lname = { required = true},
        username = {required=true, size="6..10"},
        password = {required=true, size="6..8"},
        email = {required=true, type="email"},
        age = {required=true, type="numeric", min=18}
    };
}

That easy! You can just declare these validation rules and ColdBox will validate your properties according to the rules. In this case you can see that a password must be between 6 and 10 characters long, and it cannot be blank.

By default all properties are of type string and not required

Usage

You can then use them implicitly when calling our validation methods:

validate( myUser );
validateOrFail( myUser );
PreviousConfiguration FileNextA-la-carte

Last updated 2 years ago

Was this helpful?