cborm
Install
v3.x
v3.x
  • Introduction
  • Intro
    • Release History
      • What's New With 3.9.0
      • What's New With 3.8.0
      • What's New With 3.7.0
      • What's New With 3.6.0
      • What's New With 3.5.0
      • What's New With 3.4.0
      • What's New With 3.3.0
      • What's New With 3.2.x
      • What's New With 3.1.0
      • What's New With 3.0.0
    • About This Book
    • Author
  • Getting Started
    • Overview
    • Installation
    • Basic Crud - Services
    • Basic Crud - ActiveEntity
  • Base ORM Service
    • Overview
    • Service Properties
    • Concrete Services
    • Service Methods
      • Criteria Queries
        • getRestrictions
        • newCriteria
      • Creation - Population
        • new
        • populate
        • populateFromJSON
        • populateFromQuery
        • populateFromXML
      • Counters
        • count
        • countWhere
        • exists
      • Deleting Entities
        • delete
        • deleteAll
        • deleteByID
        • deleteByQuery
        • deleteWhere
      • Entity Convenience Methods
        • getDirtyPropertyNames
        • getEntityGivenName
        • getEntityMetadata
        • getKey
        • getKeyValue
        • getPropertyNames
        • getTableName
        • isDirty
        • refresh
      • Finders
        • findit
        • findOrFail
        • findByExample
        • findWhere
        • findAll
        • findAllWhere
      • Getters
        • get
        • getOrFail
        • getAll
      • ORM Session
        • clear
        • evict
        • evictCollection
        • evictQueries
        • getSessionStatistics
        • isSessionDirty
        • merge
        • sessionContains
      • Querying
        • executeQuery
        • list
      • Saving Entities
        • save
        • saveAll
      • Utility Methods
        • autoCast
        • createService
        • idCast
        • nullValue
        • when
    • Dynamic Finders- Counters
      • Method Signatures
      • Method Expressions
      • Query Options
    • Automatic Java Types
  • Virtual Services
    • Overview
    • Service Properties
    • Concrete Virtual Services
  • Active Record
    • Active Entity Overview
    • Constructor Properties
    • Usage
    • Validation
  • Criteria Queries
    • Criteria Builder
      • Getting Started
      • Restrictions
        • Value Casting
        • SQL Restrictions
      • Modifiers
      • Results
      • Associations
      • Projections & Aggregates
    • Detached Criteria Builder
      • Getting Started
      • Projections
      • Subqueries
      • DetachedSQLProjection()
      • Criterias
      • Associations
    • Help! I'm Not Getting the Result I expected!
  • Advanced Features
    • Automatic REST Crud
    • Hibernate Logging
    • Mementifier
    • ORM Events
      • Custom Event Handler
    • Unique Property Validation
Powered by GitBook
On this page
  • Validation Functions
  • Declaring Constraints
  • Validating Constraints
  • Displaying Errors
  • Unique Property Validation

Was this helpful?

Edit on Git
Export as PDF
  1. Active Record

Validation

Validation Functions

Our active entity object will also give you access to our validation engine (cbValidation) by giving your ORM entities the following functions:

/**
 * Validate the ActiveEntity with the coded constraints -> this.constraints, or passed in shared or implicit constraints
 * The entity must have been populated with data before the validation
 *
 * @fields One or more fields to validate on, by default it validates all fields in the constraints. This can be a simple list or an array.
 * @constraints An optional shared constraints name or an actual structure of constraints to validate on.
 * @locale An optional locale to use for i18n messages
 * @excludeFields An optional list of fields to exclude from the validation.
 */
boolean function isValid(
	string fields="*",
	any constraints="",
	string locale="",
	string excludeFields=""
){

/**
* Get the validation results object.  This will be an empty validation object if isValid() has not being called yet.
*/
cbvalidation.models.result.IValidationResult function getValidationResults(){

Declaring Constraints

This makes it really easy for you to validate your ORM entities in two easy steps:

1) Add your validation constraints to the entity

2) Call the validation methods

Let's see the entity code so you can see the constraints:

models/User.cfc
component persistent="true" extends="cborm.models.ActiveEntity"{
    
    // Properties
    property name="firstName";
    property name="lastName";
    property name="email";
    property name="username";
    property name="password";

    // Validation Constraints
    this.constraints = {
        "firstName" = {required=true}, 
        "lastName"  = {required=true},
        "email"     = {required=true,type="email"},
        "username"  = {required=true, size="5..10"},
        "password"  = {required=true, size="5..10"}
    };
}

Validating Constraints

Now let's check out the handlers to see how to validate the entity via the isValid() function:

handlers/users.cfc
component{

    property name="messagebox" inject="messagebox@cbmessagebox";

    function index(event,rc,prc){
        prc.users = getInstance( "User" ).list( sortOrder="lastName asc" );
        event.setView( "users/list" );
    }

    function save(event,rc,prc){
        event.paramValue( "id", -1 );
        
        var oUser = getInstance( "User" )
            .getOrFail( rc.id )
            .populate( rc )

        if( oUser.isValid() {
            oUser.save();
            flash.put( "notice", "User Saved!" );
            relocate( "users.index" );
        }
        else{
            messagebox.error( messageArray=oUser.getValidationResults().getAllErrors() );
            editor( event, rc, prc );
        }

    }

    function editor(event,rc,prc){
        event.paramValue( "id", -1 );
        prc.user = getInstance( "User" ).getOrFail( rc.id );
        event.setView( "users/editor" );
    }

    function delete(event,rc,prc){
        event.paramValue( "id", -1 );
        getInstance( "User" )
            .deleteById( rc.id );
        flash.put( "notice", "User Removed!" );
        relocate( "users.index" );
    }
}

Please remember that the isValid() function has several arguments you can use to fine tune the validation:

  • fields

  • constraints

  • locale

  • excludeFields

Displaying Errors

You can refer back to the cbValidation docs for displaying errors:

Here are the most common methods for retreving the errors from the Result object via the getValidationResults() method:

  • getResultMetadata()

  • getFieldErrors( [field] )

  • getAllErrors( [field] )

  • getAllErrorsAsJSON( [field] )

  • getAllErrorsAsStruct( [field] )

  • getErrorCount( [field] )

  • hasErrors( [field] )

  • getErrors()

The API Docs in the module (once installed) will give you the latest information about these methods and arguments.

Unique Property Validation

We have also integrated a UniqueValidator from the validation module into our ORM module. It is mapped into WireBox as UniqueValidator@cborm so you can use it in your model constraints like so:

{ username : { validator : "UniqueValidator@cborm", required : true } }
PreviousUsageNextCriteria Builder

Last updated 3 years ago

Was this helpful?

Displaying ErrorscbValidation
Logo