PageRenderTime 163ms CodeModel.GetById 80ms app.highlight 2ms RepoModel.GetById 79ms app.codeStats 0ms

/ValidateThisCBPlugin.cfc

http://github.com/bobsilverberg/ValidateThisColdBoxPlugin
ColdFusion CFScript | 172 lines | 138 code | 34 blank | 0 comment | 4 complexity | c44f40bec41e0fa41b063e6f96e41307 MD5 | raw file
  1<!---
  2	
  3	Copyright 2009, Bob Silverberg
  4	
  5	Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 
  6	compliance with the License.  You may obtain a copy of the License at 
  7	
  8		http://www.apache.org/licenses/LICENSE-2.0
  9	
 10	Unless required by applicable law or agreed to in writing, software distributed under the License is 
 11	distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 12	implied.  See the License for the specific language governing permissions and limitations under the 
 13	License.
 14	
 15--->
 16<cfcomponent name="ValidateThisCBPlugin" hint="I am a plugin that allows ValidateThis to be accessed easily from within Coldbox." extends="coldbox.system.plugin" output="false" cache="true" cachetimeout="0">
 17
 18	<cffunction name="init" access="public" returntype="any" output="false">
 19		<cfargument name="controller" type="any" required="true">
 20		<cfset var key = 0 />
 21		<cfset var setting = 0 />
 22		<cfset var ValidateThisConfig = StructNew() />
 23		<cfset super.Init(arguments.controller) />
 24		<cfset setpluginName("ValidateThis Plugin") />
 25		<cfset setpluginVersion("0.2") />
 26		<cfset setpluginDescription("I allow ValidateThis to be accessed easily from within Coldbox.") />
 27		
 28		<!--- 
 29		check for ValidateThis setting defined in Coldbox.xml.cfm 
 30		this setting should be in the format:
 31			<Setting name="ValidateThisConfig" value="{JSRoot:'/js/',defaultFormName:'formToValidate'}" />
 32		--->
 33		<cfif settingExists("ValidateThisConfig")>
 34			<!--- settings found so use them to configure ValidateThis --->
 35			<cfset ValidateThisConfig = getSetting("ValidateThisConfig") />
 36		</cfif>
 37		
 38		<!--- Create a ValidateThisConfig struct from data in the Coldbox settings --->
 39		<cfloop list="VT_TranslatorPath,VT_LocaleLoaderPath,VT_BOValidatorPath,VT_DefaultJSLib,VT_JSRoot,VT_defaultFormName,VT_definitionPath,VT_localeMap,VT_defaultLocale,VT_abstractGetterMethod,VT_ExtraRuleValidatorComponentPaths" index="key">
 40		
 41			<cfif settingExists(key)>
 42				<cfset setting = Replace(key,"VT_","")>
 43				<!--- throw an error if the setting is already defined --->		
 44				<cfif settingExists(key) AND StructKeyExists(ValidateThisConfig, setting)>
 45					<cfthrow type="ValidateThisCBPlugin.init.duplicateSetting"
 46						message="Duplicate setting in ColdBox.xml.cfm" 
 47						detail="The setting '#setting#' is already defined in the 'ValidateThisConfig' setting" />
 48				<cfelse>
 49					<cfset ValidateThisConfig[setting] = getSetting(key) />
 50				</cfif>
 51			</cfif>
 52		</cfloop>
 53		<!--- If no definitionPath was defined, use the ModelsPath and (optionally) the ModelsExternalLocationPath from the ColdBox config --->
 54		<cfif NOT StructKeyExists(ValidateThisConfig,"definitionPath")>
 55			<cfset ValidateThisConfig.definitionPath = getSetting("ModelsPath") & "/" />
 56			<cfif settingExists("ModelsExternalLocationPath") AND Len(getSetting("ModelsExternalLocationPath"))>
 57				<cfset ValidateThisConfig.definitionPath = ListAppend(ValidateThisConfig.definitionPath, getSetting("ModelsExternalLocationPath")) />
 58			</cfif>
 59		</cfif>
 60		<cfset variables.ValidateThis = CreateObject("component","ValidateThis.ValidateThis").init(ValidateThisConfig) />
 61		<cfreturn this>
 62	</cffunction>
 63
 64	<!--- Public methods - These are explicitly created to provide a concrete API for the Plugin --->
 65
 66	<cffunction name="validate" access="public" output="false" returntype="any" hint="I am used to perform server-side validations on an object">
 67		<cfargument name="theObject" type="any" required="true" />
 68		<cfargument name="objectType" type="any" required="false" />
 69		<cfargument name="Context" type="any" required="false" />
 70		<cfargument name="Result" type="any" required="false" />
 71
 72		<cfreturn variables.ValidateThis.validate(argumentCollection=arguments) />
 73	</cffunction>
 74
 75	<cffunction name="setupValidationSI" access="public" output="false" returntype="void" hint="Creates the default form setup for jQuery validation using the scriptInclude plugin.">
 76		<!--- Based on a Plugin created by Craig McDonald --->
 77		<!--- Note: This has not been tested --->
 78		<cfargument name="objectList" type="any" required="true" hint="One or more objects to validate. As a list" />
 79		<cfargument name="context" type="any" required="false" default="" hint="The context of the form to validate" />
 80		<cfargument name="locale" type="Any" required="false" default="" />
 81		<cfargument name="formName" type="Any" required="false" default="" />
 82		<cfargument name="loadMainLibrary" type="Any" required="false" default="false" />
 83
 84		<cfset var scriptInclude = getMyPlugin("scriptInclude") />
 85		<cfset var object = 0 />
 86		<cfif arguments.loadMainLibrary>
 87			<cfset scriptInclude.addResource(file='jquery-1.3.2.min',type='JS',path='') />
 88		</cfif>
 89		<cfset scriptInclude.addResource(file='jquery.field.min',type='JS',path='') />
 90		<cfset scriptInclude.addResource(file='jquery.validate.pack',type='JS',path='') />
 91		<cfset scriptInclude.addScript(script=getInitializationScript(JSLib="jQuery",JSIncludes=false,locale=arguments.locale)) />
 92		<cfloop index="object" list="#arguments.objectList#">		
 93			<cfset scriptInclude.addScript(script=getValidationScript(JSLib="jQuery",objectType=object,Context=arguments.context,formName=arguments.formName,locale=arguments.locale)) />
 94		</cfloop>
 95	
 96	</cffunction>
 97
 98	<cffunction name="getValidationScript" returntype="any" access="public" output="false" hint="I generate client-side validations scripts for an object">
 99		<cfargument name="theObject" type="any" required="false" />
100		<cfargument name="objectType" type="any" required="false" />
101		<cfargument name="Context" type="any" required="false" />
102		<cfargument name="formName" type="any" required="false" />
103		<cfargument name="JSLib" type="any" required="false" />
104		<cfargument name="locale" type="Any" required="false" />
105
106		<cfreturn variables.ValidateThis.getValidationScript(argumentCollection=arguments) />
107	</cffunction>
108	
109	<cffunction name="getInitializationScript" returntype="any" access="public" output="false" hint="I generate JS statements required to setup client-side validations for VT.">
110		<cfargument name="JSLib" type="any" required="false" />
111		<cfargument name="JSIncludes" type="Any" required="no" default="true" />
112		<cfargument name="locale" type="Any" required="no" default="" />
113
114		<cfreturn variables.ValidateThis.getInitializationScript(argumentCollection=arguments) />
115	</cffunction>
116
117	<cffunction name="getRequiredProperties" access="public" output="false" returntype="any" hint="I return a structure containing the name of all of the required properties for the given context.">
118		<cfargument name="theObject" type="any" required="false" />
119		<cfargument name="objectType" type="any" required="false" />
120		<cfargument name="Context" type="any" required="false" />
121
122		<cfreturn variables.ValidateThis.getRequiredProperties(argumentCollection=arguments) />
123	</cffunction>
124
125	<cffunction name="getRequiredFields" access="public" output="false" returntype="any" hint="I return a structure containing the name of all of the required fields for the given context.">
126		<cfargument name="theObject" type="any" required="false" />
127		<cfargument name="objectType" type="any" required="false" />
128		<cfargument name="Context" type="any" required="false" />
129
130		<cfreturn variables.ValidateThis.getRequiredFields(argumentCollection=arguments) />
131	</cffunction>
132
133	<cffunction name="getAllContexts" access="public" output="false" returntype="any" hint="I return a Struct containing all of the rules defined for the object.">
134		<cfargument name="theObject" type="any" required="false" />
135		<cfargument name="objectType" type="any" required="false" />
136
137		<cfreturn variables.ValidateThis.getAllContexts(argumentCollection=arguments) />
138	</cffunction>
139
140	<cffunction name="addRule" returnType="void" access="public" output="false" hint="I am used to add a rule via CFML code">
141		<cfargument name="propertyName" type="any" required="true" />
142		<cfargument name="valType" type="any" required="true" />
143		<cfargument name="theObject" type="any" required="false" />
144		<cfargument name="objectType" type="any" required="false" />
145		<cfargument name="clientFieldName" type="any" required="false" />
146		<cfargument name="propertyDesc" type="any" required="false" />
147		<cfargument name="condition" type="Struct" required="false" />
148		<cfargument name="parameters" type="Struct" required="false" />
149		<cfargument name="contexts" type="any" required="false" />
150		<cfargument name="failureMessage" type="any" required="false" />
151		<cfargument name="formName" type="any" required="false" />
152
153		<cfreturn variables.ValidateThis.addRule(argumentCollection=arguments) />
154	</cffunction>
155
156
157	<cffunction name="onMissingMethod" access="public" output="false" returntype="Any" hint="I am used to pass all method calls to the composed ValidateThis object.">
158		<cfargument name="missingMethodName" type="any" required="true" />
159		<cfargument name="missingMethodArguments" type="any" required="true" />
160
161		<cfset returnValue = "" />
162		<cfinvoke component="#variables.ValidateThis#" method="#arguments.missingMethodName#" argumentcollection="#arguments.missingMethodArguments#" returnvariable="returnValue" />
163		<cfif NOT IsDefined("returnValue")>
164			<cfset returnValue = "" />
165		</cfif>
166		<cfreturn returnValue />
167		
168	</cffunction>
169
170</cfcomponent>
171	
172