Blog

ColdBox Validation is Now a Module

Scott Coldwell January 28, 2015

Spread the word

Scott Coldwell

January 28, 2015

Spread the word


Share your thoughts

The new modular approach of ColdBox 4.0 means much of its built-in functionality has been moved to separate, installable modules. One of the many new modules introduced with ColdBox 4.0 is the Validation Module. To install, simply fire up CommandBox:

box install cbvalidation

This module replaces the previously-inbuilt validation functionality of ColdBox and the validator Plugin. The ValidationManager is still available under this WireBox mapping: ValidationManager@cbvalidation. It gives you the following methods in every handler, view, layout, etc:

  • validateModel()
  • getValidationManager()

 

If you aren't familiar, ColdBox's Validation Module provides you with a unified approach to object validation. It is based it on several interfaces, so you can also use any validation framework in your ColdBox applications as long as they implement the required interfaces. Hyrule by Dan Vega is the easiest to port as it was the framework that inspired us. ColdBox validation allows you to declaratively specify validation rules for properties or fields in an object. The constraints can exist inside of the target object or you can define constraints in your ColdBox configuration file so you can reuse validation constraints or as we call them: shared constraints.

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}
	};
}

 

Below are all the currently supported constraints:

propertyName = {
	// required field or not, includes null values
	required : boolean [false],
	
	// specific type constraint, one in the list.
	type  : (ssn,email,url,alpha,boolean,date,usdate,eurodate,numeric,GUID,UUID,integer,string,telephone,
                zipcode,ipaddress,creditcard,binary,component,query,struct,json,xml)

	// size or length of the value which can be a (struct,string,array,query)
	size  : numeric or range, eg: 10 or 6..8
	
	// range is a range of values the property value should exist in
	range : eg: 1..10 or 5..-5
	
	// regex validation
	regex : valid no case regex
	
	// same as another property
	sameAs : propertyName
	
	// same as but with no case
	sameAsNoCase : propertyName
	
	// value in list
	inList : list
	
	// discrete math modifiers
	discrete : (gt,gte,lt,lte,eq,neq):value
	
	// UDF to use for validation, must return boolean accept the incoming value and target object, validate(value,target):boolean
	udf = variables.UDF or this.UDF or a closure.
	
	// Validation method to use in the target object must return boolean accept the incoming value and target object 
	method : methodName
	
	// Custom validator, must implement coldbox.system.validation.validators.IValidator
	validator : path or wirebox id, example: 'mypath.MyValidator' or 'id:MyValidator'
	
	// min value
	min : value
	
	// max value
	max : value
}

Add Your Comment

(1)

Apr 10, 2015 13:38:46 UTC

by Jim Priest

Since this is now installed via a module and no longer in the core this is incorrect: Custom validator, must implement coldbox.system.validation.validators.IValidator

Recent Entries

The 12 Days of Christmas - ITB 2022 Video Release— Day 2 - New Modules

The 12 Days of Christmas - ITB 2022 Video Release— Day 2 - New Modules

It's that time of year again. Trees are lit, presents are being wrapped, and relatives are coming to visit. That's right, it's time again for the 12 days of Christmas-- 2022 ITB Video Release Edition! 

Today, Day 2's Videos from Into the Box 2022 Conference, are all newly announced modules.

Gavin Pickin
Gavin Pickin
December 09, 2022
The 12 Days of Christmas - ITB 2022 Video Release— Day 1

The 12 Days of Christmas - ITB 2022 Video Release— Day 1

It's that time of year again.  Trees are lit, presents are being wrapped, and relatives are coming to visit.  That's right, it's time again for the 12 days of Christmas-- 2022 ITB Video Release Edition!

For the next 12 business days up until Christmas, we'll be releasing a series of related videos each day on the CFCasts site for our CFCasts Subscribers, and a announcement with titles, descriptions, and links to the videos, right here on the Ortus blog.  Consider it our early Christmas gift to you.

Gavin Pickin
Gavin Pickin
December 08, 2022
CommandBox Docker v3.6.3 Images Released

CommandBox Docker v3.6.3 Images Released

CommandBox 3.6.3 Docker images released



Today we are pleased to announce the release of version 3.6.3 of our CommandBox Docker images, which contains significant upgrades to the underlying CommandBox engine.

Most significantly, this release upgrades the CommandBox binary to 5.7.0. Read the Release Notes for this version here. This image version also adds new distro builds for the RHEL Universal Base Image. These images may be utilized by using the ubi9 in the image tag in the same way the alpine base images currently do.

Jon Clausen
Jon Clausen
December 08, 2022