FW/1, WireBox and ValidateThis
I've been trying out two new frameworks today, FW/1 and WireBox, along with ValidateThis.
FW/1 is a really nice lightweight MVC framework and WireBox is a Dependancy Injection framework. Getting FW/1 has a handy setBeanFactory method which allows you to use any BeanFactory that responds to containsBean and getBean calls. WireBox doesn't have these methods, but it does ship with an adapter which lets you expose those methods. These adapters are primary for replacing LightWire or ColdSpring with WireBox, but as this is a new applicaiton I'm building, I'm using the bundled wirebox.system.ioc.adapters.WireBoxAdapter. In my Application.cfc I use the setupApplication() method of FW/1 to load and configure WireBox.
// executes when application starts
function setupApplication()
{
var beanFactory = new wirebox.system.ioc.adapters.WireBoxAdapter( definitionFile="model.config.binder" );
beanFactory.createFactory();
setBeanFactory( beanFactory );
}
You may notice that I'm passing the definitionFile argument into the WireBoxAdapter. This is the dotted path to my binder, which is where I setup my relationships and configure ValidateThis. My binder looks like:
component extends="wirebox.system.ioc.config.Binder"
{
function configure()
{
// map all cfcs in this directory and subdirectories
mapDirectory( "model" );
// define and configure validatethis
mapPath("validatethis.ValidateThis").asSingleton().initWith(
{
JSIncludes = "false"
}
);
}
}
I can now have ValidateThis injected into my controller by simply creating a setValidateThis method like so:
component
{
variables.fw = '';
/* CONSTRUCTOR
------------------------------------------------------------------------------ */
function init( required any fw )
{
variables.fw = arguments.fw;
}
/* EXPLICIT
------------------------------------------------------------------------------ */
function dosave( required struct rc )
{
rc.foo = EntityNew( "Foo" );
variables.fw.populate( rc.foo );
rc.validationresult = variables.validatethis.validate( foo );
if ( rc.validationresult.hasErrors() )
{
variables.fw.redirect('foo.maintain','validationresult,foo');
}
else
{
transaction
{
EntitySave( foo );
}
rc.message = ["foo saved"];
variables.fw.redirect('foo.bar','message');
}
}
/* DI
------------------------------------------------------------------------------ */
function setValidateThis( ValidateThis )
{
variables.ValidateThis = arguments.ValidateThis;
}
}
It took me a while to figure this out as the docs, but as you can see it's pretty simple and a very powerful combination.
- Posted in:
- ColdFusion
- ValidateThis


One thing: shouldn't it read:
rc.validationresult = variables.validatethis.validate( foo );
Comment by Seb Duggan – March 23, 2011
Thanks for pointing out the Adapter for WireBox.
Comment by John Allen – March 23, 2011
component accessors="true" {
property validateThis;
}
Also, your example references validateThis inside the request collection (rc.validatethis), which I believe is incorrect.
Comment by Tony Nelson – March 23, 2011
I believe that this will be possible in FW/1 v2, as a result of it being limited to more recent CFML engines (CF9, etc).
Comment by Seb Duggan – March 23, 2011
I did try using property first off and was surprised it didn't work. I haven't had a chance to dig around in the FW/1 code to see what it does. From what Sean said at SOTR recently version 2.0 is going to be awesome!
Comment by John Whish – March 23, 2011
I find it hard to believe that accessors don't allow for autowiring, since the accessors simply create the get/set methods for you. There's really no way for FW/1 (or WireBox) to tell if those methods are manually created or not...
Comment by Tony Nelson – March 23, 2011
<cfloop item="key" collection="#arguments.cfc#">
This only seems (on CF9.01 anyway) to loop through the "concrete" methods. I guess this is done for compatibility with other CFML engines.
Comment by John Whish – March 23, 2011
Comment by Stephen Weyrick – March 24, 2011
I asked Sean about this and FW/1 v2 will support implicit accessors. The reason why it doesn't use metadata is because of performance and inheritance. Also, when FW/1 was released implicit accessors didn't exist.
Comment by John Whish – March 25, 2011