Use beforeSave() for validation and display error message

30 views
Skip to first unread message

Xiaofeng Liu

unread,
Aug 19, 2013, 4:02:46 AM8/19/13
to farcr...@googlegroups.com
Hi,

Within webtop on a custom type, if I'd like to use beforeSave() to check, for example the uniqueness of username before my type record gets saved, how do I bring back that validation message to display on the farcry scaffolded list view or maybe the add/edit view?

--
Best regards,

Xiaofeng

Chris Kent

unread,
Aug 19, 2013, 4:42:19 AM8/19/13
to farcr...@googlegroups.com
You would probably be better off using a validate method inside your content type. you can set these to validate individual properties. Default is to name the function "ftValidate+propertyname", e.g. ftValidateUserID

Look at ftValidateUserID in /core/packages/types/farUser.cfc for an example to check for unique userID.

Chris

Xiaofeng Liu

unread,
Aug 20, 2013, 1:02:19 AM8/20/13
to farcr...@googlegroups.com
Hi Chris,

Thanks for pointing to the use of form tools validation methods. However, how would I get the error message displayed on my custom type edit form?

I have the property set to:

<cfproperty ftseq="50" ftFieldSet="Login" type="string" ftLabel="Username" name="username" ftType="string" ftValidation="required" bLabel="true">

Then I have my validation method as:

<cffunction name="ftValidateUsername" access="public" output="true" returntype="struct" hint="This will return a struct with bSuccess and stError">
<cfargument name="objectid" required="true" type="string" hint="The objectid of the object that this field is part of.">
<cfargument name="typename" required="true" type="string" hint="The name of the type that this field is part of.">
<cfargument name="stFieldPost" required="true" type="struct" hint="The fields that are relevent to this field type.">
<cfargument name="stMetadata" required="true" type="struct" hint="This is the metadata that is either setup as part of the type.cfc or overridden when calling ft:object by using the stMetadata argument.">
<cfset var stResult = structNew()>
<cfset var qDuplicate = queryNew("blah")>
<cfset stResult = createObject("component", application.formtools["field"].packagePath).passed(value=stFieldPost.Value) />
<!--- --------------------------- --->
<!--- Perform any validation here --->
<!--- --------------------------- --->
<cfquery datasource="#application.dsn#" name="qDuplicate">
SELECT objectid from userProfile
WHERE upper(username) = '#ucase(stFieldPost.Value)#'
</cfquery>
<cfif qDuplicate.RecordCount>
<!--- DUPLICATE USERNAME --->
<cfset stResult = createObject("component", application.formtools["field"].packagePath).failed(value="#arguments.stFieldPost.value#", message="The username you have selected is already taken.") />
<!--- <cfdump var="#stResult#"><cfabort> --->
</cfif>
<!--- ----------------- --->
<!--- Return the Result --->
<!--- ----------------- --->
<cfreturn stResult>
</cffunction>

If I dump out the stResult, I can see ftValidateUsername is invoked and it correctly detected the duplicates and set the error message:

struct
BSUCCESS false
STERROR
struct
CLASS validation-advice
MESSAGE The username you have selected is already taken.
VALUE flyingturtle123
       

However, without abort execution, it still saved the record and not displaying the validation message anywhere.

Am I missing sth here?




--
You received this message cos you are subscribed to "farcry-dev" Google group.
To post, email: farcr...@googlegroups.com
To unsubscribe, email: farcry-dev+...@googlegroups.com
For more options: http://groups.google.com/group/farcry-dev
--------------------------------
Follow us on Twitter: http://twitter.com/farcry
---
You received this message because you are subscribed to the Google Groups "farcry-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to farcry-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Best regards,

Xiaofeng,^_^

Xiaofeng Liu

unread,
Aug 20, 2013, 7:18:45 PM8/20/13
to farcr...@googlegroups.com
Does anyone have an idea how exactly ftValidatePropertyName method work?

I'm just wondering did I miss any steps to get the validation error displayed within the webtop or is this a bug in FarCry?

Any thoughts would be much appreciated.

--
Best regards,

Xiaofeng,^_^

Blair McKenzie

unread,
Aug 20, 2013, 8:11:41 PM8/20/13
to Unname
Take a look at the formtool components in core (/packages/formtools). All validation functions accept the same arguments and should return the same results as the ones those components have.

Blair

Xiaofeng Liu

unread,
Aug 27, 2013, 8:58:43 PM8/27/13
to farcr...@googlegroups.com
Hi,

As Chris Kent suggested, I actually took a copy of the code in 'ftValidateUserID' method in /core/packages/types/farUser.cfc and adjusted it to do my checking logic for username. The validation method did accept the same arguments as those ones in core and when I dumped the result structure it correctly set the bSuccess to 'false' and the error message accordingly when validation failed. However, after that it ignored the result and still saved the type record with a duplicate username. I have been stuck on this one for quite a while. It looks like something is missing here.

Code is as below:

<cfproperty ftseq="50" ftFieldSet="Login" type="string" ftLabel="Username" name="username" ftType="string" ftValidation="required" bLabel="true">



Chris Kent

unread,
Aug 28, 2013, 4:36:31 AM8/28/13
to farcr...@googlegroups.com
What version of FarCry are you using?

Chris.

Xiaofeng Liu

unread,
Aug 28, 2013, 7:07:43 AM8/28/13
to farcr...@googlegroups.com
I'm using 6-0-15. Currently I could not get server side validation fully working. I have to put a workaround that is to abort execution within the validation method and output the validation message. Otherwise, after the validation method's execution, it just ignored what was set in the stResults.

Ashwani Singh

unread,
Aug 28, 2013, 11:19:40 AM8/28/13
to farcr...@googlegroups.com
try to use <cfimport taglib="/farcry/core/tags/formtools/" prefix="ft" /> instead of  <cfimport taglib="/farcry/core/tags/wizard/" prefix="wiz" />

and add <ft:serverSideValidation /> before <wiz:processwizard excludeAction="Cancel"> in Edit Function.

server side validation working only in form not in wizard. (I am using farcry 6.2.8)




Reply all
Reply to author
Forward
0 new messages