Skip to content

Commit

Permalink
Implementing Server Side validations via VT
Browse files Browse the repository at this point in the history
  • Loading branch information
bobsilverberg authored and mhenke committed Nov 20, 2009
1 parent 2edcc47 commit 70186fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion demo/controllers/validateThis.cfc
Expand Up @@ -9,7 +9,7 @@
<cfset User = model("user").new(params.userTo) />

<!--- Validate the object using ValidateThis! --->
<cfset Result = application.ValidateThis.validate(theObject=UserTO,objectType="User",Context=params.Context,results=params.userTo) />
<cfset Result = application.ValidateThis.validate(theObject=User,objectType="User",Context=params.Context,results=params.userTo) />

<!--- If validations passed, save the record --->
<cfif Result.getIsSuccess()>
Expand Down
13 changes: 13 additions & 0 deletions demo/models/User.cfc
Expand Up @@ -5,4 +5,17 @@
<cfset belongsTo(name="UserGroup", foreignKey="UserGroupId")>
</cffunction>

<cffunction name="CheckDupNickname" access="public" output="false" returntype="any" hint="Checks for a duplicate UserName.">

<!--- This is just a "mock" method to test out the custom validation type --->
<cfset var ReturnStruct = StructNew() />
<cfset ReturnStruct.IsSuccess = false />
<cfset ReturnStruct.FailureMessage = "That Nickname has already been used. Try to be more original!" />
<cfif $propertyvalue("Nickname") NEQ "BobRules">
<cfset ReturnStruct = StructNew() />
<cfset ReturnStruct.IsSuccess = true />
</cfif>
<cfreturn ReturnStruct />
</cffunction>

</cfcomponent>
10 changes: 9 additions & 1 deletion demo/models/user.xml
Expand Up @@ -2,7 +2,7 @@
<validateThis xsi:noNamespaceSchemaLocation="validateThis.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<conditions>
<condition name="MustLikeSomething"
serverTest="getLikeCheese() EQ 0 AND getLikeChocolate() EQ 0"
serverTest="$propertyvalue('LikeCheese') EQ 0 AND $propertyvalue('LikeChocolate') EQ 0"
clientTest="$(&quot;[name='LikeCheese']&quot;).getValue() == 0 &amp;&amp; $(&quot;[name='LikeChocolate']&quot;).getValue() == 0;" />
</conditions>
<contexts>
Expand All @@ -27,15 +27,19 @@
<param maxlength="10" />
</rule>
</property>
<!-- Removing until I can figure out how to add a non-persistent property into a model object
<property name="VerifyPassword" desc="Verify Password">
<rule type="required" contexts="*" />
<rule type="equalTo" contexts="*">
<param ComparePropertyName="UserPass" />
</rule>
</property>
-->
<!-- Removing until I can figure out how to reference an embedded object as a property
<property name="UserGroup" desc="User Group" clientfieldname="UserGroupId">
<rule type="required" contexts="*" />
</property>
-->
<property name="Salutation">
<rule type="required" contexts="Profile" />
<rule type="regex" contexts="*"
Expand All @@ -52,21 +56,25 @@
<param DependentPropertyName="FirstName" />
</rule>
</property>
<!-- Removing because the LikeCheese property doesn't exist in the User object. Why?
<property name="LikeOther" desc="What do you like?">
<rule type="required" contexts="*" condition="MustLikeSomething"
failureMessage="If you don't like Cheese and you don't like Chocolate, you must like something!">
</rule>
</property>
-->
<property name="HowMuch" desc="How much money would you like?">
<rule type="numeric" contexts="*" />
</property>
<property name="AllowCommunication" desc="Allow Communication" />
<!-- Removing because the AllowCommunication property doesn't exist in the User object. Why?
<property name="CommunicationMethod" desc="Communication Method">
<rule type="required" contexts="*"
failureMessage="If you are allowing communication, you must choose a communication method.">
<param DependentPropertyName="AllowCommunication" />
<param DependentPropertyValue="1" />
</rule>
</property>
-->
</objectProperties>
</validateThis>

0 comments on commit 70186fd

Please sign in to comment.