I would like to write a server-side validation code to validate all user inputs on my page. I would like to keep all these server-side validations from the presentation layer. Now I am looking into creating a component and keep all validation functions inside it. I will use <cfinvoke>
tag to access validation methods on my user page. But the problem is I have to apply validation on around 50 user inputs and need to use <cfinvoke>
tag 50 times. Can anyone tell me that using <cfinvoke>
many times will affect performance or is there any other better approach that I can go with?
Asked
Active
Viewed 333 times
2

Miguel-F
- 13,450
- 6
- 38
- 63

kamil hussain
- 53
- 6
1 Answers
3
There's no noticable overhead using <cfinvoke>
. But I would still recommend you to avoid it here. Instead consider this (example):
Validator.cfc
<cfcomponent>
<cffunction name="validateX" access="public" ...>
...
</cffunction>
<cffunction name="validateY" access="public" ...>
...
</cffunction>
<cffunction name="validateZ" access="public" ...>
...
</cffunction>
</cfcomponent>
controller.cfm
<cfset validator = new Validator()>
<cfset validator.validateX(...)>
<cfset validator.validateY(...)>
<cfset validator.validateZ(...)>
...
Now you can easily work with the validation result.
If you return boolean:
<cfif validator.validateX(...)>
...
<cfelse>
...
</cfif>
If you return an array with errors:
<cfset errors = []>
<cfset errors.addAll( validator.validateX(...) )>
<cfset errors.addAll( validator.validateY(...) )>
<cfset errors.addAll( validator.validateZ(...) )>
etc.
<cfinvoke>
creates an instance of the Class (new Validator()
) and invokes the method validateX(...)
the same way. The major difference is: the instance is created on every <cfinvoke>
anew and the return has to be specified as input variable (returnVariable
), which is cumbersome in most cases.

Alex
- 7,743
- 1
- 18
- 38
-
Thanks Alex. Can you explain the difference of using
and your way? I am new to Cold fusion. Your way seems to more readable. – kamil hussain Oct 29 '17 at 13:10 -
Yes, the `new` operator was freshly introduced in CF9. I added an explaination to my answer at the bottom regarding the different approaches. – Alex Oct 29 '17 at 13:16
-
Thanks Alex for your help. – kamil hussain Oct 29 '17 at 14:47
-
One other note: `New` will call the `init()` function of your method. Of course, you could also use `createObject()` or `cfobject`, too, but `New` is most often my choice. – Shawn Oct 30 '17 at 14:08