Mura FW/1 and ValidateThis Plugin

I had a few hours today which I set aside to learn more about FW/1 and ValidateThis. I wanted to use both of these frameworks within a Mura Plug-in I had created. I have never used ValidateThis, so I wondered how hard it would be to add it into the fw/1 Plug-in. I am not saying below is the best way, it's my first attempt and I welcome ideas :)

Application.cfc
view plain print about
1<cfcomponent extends="fw1">
2    <cfinclude template="../../config/applicationSettings.cfm" />
3    <cfinclude template="../../config/mappings.cfm" />
4    <cfinclude template="../mappings.cfm" />
5    <cfinclude template="fw1config.cfm" />
6    <!--- ValidateThis Setup Facade Type --->
7    <cfset THIS.mappings["/ValidateThis"]="#expandPath( '/plugins' )#/eventManagerApp/com/utility/ValidateThis">
8
9    <!--- ********************** fw/1-specific *************************** --->
10    <cffunction name="setupApplication" output="false">
11        <cfscript>
12            var local = StructNew();
13        
</cfscript>
14        <cflock type="exclusive" timeout="50">
15            <cfscript>
16                // Set the plugin config into the application scope
17
                application[variables.framework.applicationKey].pluginConfig = application.pluginManager.getConfig(ID=variables.framework.applicationKey);
18                
19                // Create a local reference to the application pluginConfig
20
                local.pc = application[variables.framework.applicationKey].pluginConfig;
21                
22                // Find your coldspring XML
23
                xmlPath = "#expandPath( '/plugins' )#/#local.pc.getDirectory()#/coldspring/coldspring.xml.cfm";
24                xml = FileRead("#xmlPath#");
25                
26                // Create Coldspring bean factory
27
                local.beanFactory=createObject("component","coldspring.beans.DefaultXmlBeanFactory").init();
28                local.beanFactory.loadBeansFromXmlRaw( xml );
29                
30                //This line is if you want to inject mura's bean factory into yours
31
                local.beanFactory.setParent(application.servicefactory);
32                
33                //Set you finished bean factory as a application variable in the pluginConfig
34
                local.pc.getApplication().setValue("beanFactory", local.beanFactory);
35                
36                //set Bean factory in FW/1
37
                setBeanFactory( local.pc.getApplication().getValue("beanFactory") );
38
39                
40            
</cfscript>
41        </cflock>
42    </cffunction>
Coldspring.xml
view plain print about
1<bean id="ValidateThisConfig" class="coldspring.beans.factory.config.MapFactoryBean">
2    <property name="sourceMap">
3        <map>
4            <entry key="definitionPath"><value>/eventManagerApp/com/validate/</value></entry>
5            <entry key="JSRoot"><value>/assets/js/</value></entry>
6            </map>
7        </property>
8    </bean>
9        
10<bean id="ValidateThis" class="eventManagerApp.com.utility.ValidateThis.ValidateThis">
11    <constructor-arg name="ValidateThisConfig"><ref bean="ValidateThisConfig" /></constructor-arg>
12</bean>

The first thing you will notice is that I added an application mapping in the application.cfc file (within the plug-in). Becuase this is a plug-in, I wanted everything to install together.

view plain print about
1<!--- ValidateThis Setup Facade Type --->
2<cfset THIS.mappings["/ValidateThis"]="#expandPath( '/plugins' )#/eventManagerApp/com/utility/ValidateThis">

I wanted to use the Coldspring implementation of validateThis (as I was already using CS with the Plug-in). Before I could do this I needed to extend the FW/1 application by adding the PluginConfig to it's application scope. This would mean I could have access from within the framework, and not have to go outside. I found a really good post by Grant Shepert showing an example of how to achieve this.

view plain print about
1// Set the plugin config into the application scope
2application[variables.framework.applicationKey].pluginConfig = application.pluginManager.getConfig(ID=variables.framework.applicationKey);
3                
4// Create a local reference to the application pluginConfig
5local.pc = application[variables.framework.applicationKey].pluginConfig;
6                
7// Find your coldspring XML
8xmlPath = "#expandPath( '/plugins' )#/#local.pc.getDirectory()#/coldspring/coldspring.xml.cfm";
9xml = FileRead("#xmlPath#");
10                
11// Create Coldspring bean factory
12                local.beanFactory=createObject("component","coldspring.beans.DefaultXmlBeanFactory").init();
13local.beanFactory.loadBeansFromXmlRaw( xml );
14                
15//This line is if you want to inject mura's bean factory into yours
16local.beanFactory.setParent(application.servicefactory);
17                
18//Set you finished bean factory as a application variable in the pluginConfig
19local.pc.getApplication().setValue("beanFactory", local.beanFactory);
20                
21//set Bean factory in FW/1
22setBeanFactory( local.pc.getApplication().getValue("beanFactory") );

With this done I added a "before()" function in my base controller for fw/1. I use the fw.getPluginConfig() function to set PluginConfig into my request context (rc), as Grant shows in his example.

This now means I could run the following code in a view.

view plain print about
1rc.pluginConfig.getApplication().getValue( "beanFactory").getbean("ValidateThis").validate(rc.venueDetails).GETERRORS()

Bang, done!

You will notice that I don't pass the object type in the validate function. This is because I added a getObjectType function within each entity that tells ValidateThis what the name of the rule file will be.

venue.cfc entity
view plain print about
1public function getObjectType() {
2        return "venue";

That's it, so I am left with two places I need to setup my rules each time. com/entity - for all ORM entities and com/validate - for all rules .xml files or Json.

There is a few more things I need to do here, but for for a quick and dirty example you can see how easy this all ties in together.

Jan31

Comments 0