Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

cffunction problem

Posted on 2012-03-21
5
Medium Priority
?
354 Views
Last Modified: 2012-06-27
Hello experts.
I need help to add a validation code i had in a validation.cfm file ,in a cffunction now.
My previous validation was:
1) form validation:
<cfset objgetAdd = CreateObject("component", "com.getlanguages").init(dsn = "#request.dsn#")>
<cfset getlanguages = objgetAdd.fgetlanguages(ascdesc = "desc",languages_active =1)>
<!--- Check if all languages text exists --->
 <cfloop query="getlanguages">
   <cfparam name="form.newcategory#languages_id#" default="">
   <cfset thisNewCategory = form["NewCategory" & languages_id]>
   <cfif len(trim(thisNewCategory)) is 0 AND languages_active EQ 1>
   <span class="errormessage">error:</span> Required field.<br>
   <cfabort>
  </cfif>
 </cfloop>
 ..........

My new code now is:
 cfm page
<cfset objcategories = CreateObject("component", "admin.com.categories").init(dsn = "#request.dsn#")>
<cfif isdefined("form.action") AND form.action EQ "Add" >
 <cfset qryinsert = objcategories.fvalidateandinsertcategory(structform="#form#")>
</cfif>


function.....
 <cffunction name="fvalidateandinsertcategory" access="public" returntype="any">
     <cfargument name="structform" type="struct">
      <cfset var totalerrors = 0 />
      <cfset var errorlist = ''>
      <cfset getlanguages = fgetlanguages(ascdesc = "desc",languages_active =1)>
        <!--- Check if all languages text exists --->
       <cfloop query="getlanguages">
        <!--- <cfargument name="arguments.newcategory#languages_id#" default=""> --->
         <cfset thisNewCategory = arguments["NewCategory" & languages_id]>
         <cfif  len(trim(thisNewCategory)) is 0 AND languages_active EQ 1>
          <cfset var totalerrors = totalerrors + 1>
          <cfset var errorlist = ListAppend(errorlist,' required fields!','|') />
         </cfif>
      </cfloop>
         <cfset resultstruct = structnew()/>
         <cfset resultstruct.totalerrors = totalerrors />
         <cfset resultstruct.errorlist = errorlist />
          <cfif totalerrors GT 0 >
           <cfset resultstruct.success = "no" />
         <cfelse>
         </cfif>
     <cfreturn resultstruct>
    </cffunction>
Any help?
0
Comment
Question by:Panos
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 2000 total points
ID: 37747258
I had to make some guesses, but this looks like what you were going for.
One odd thingis the error text, if there is more than one error, the message will just say

 " required fields!  required fields!  required fields! "

(btw, I don't think it's professional to yell at your users, leave out the ! )

 <cffunction name="fvalidateandinsertcategory" access="public" returntype="any">
     <cfargument name="p_formStructure" type="struct">

      <cfset var formStructure = arguments.p_formStructure>
      <cfset var fieldName = "">
      <cfset var totalerrors = 0>
      <cfset var errorlist = "">
      <cfset var getlanguages = fgetlanguages(ascdesc = "desc",languages_active =1)>
      <cfset var resultstruct = structnew()>

      <cfset resultstruct.success = "yes"> <!---- assume success ---->

      <!--- Check if all languages text exists --->
      <cfloop query="getlanguages">
          <cfset fieldName = "NewCategory" & getlanguages.languages_id>
          <cfif NOT structKeyExists(formStructure,fieldName)
                 or len(trim(formStructure[fieldName])) eq 0 
                 or val(getlanguages.languages_active) eq 0>
             <cfset var totalerrors = totalerrors + 1>
             <cfset var errorlist = ListAppend(errorlist,' required fields!','|')> <!---- same message every time, repeated? ----->
          </cfif>
        </cfloop>
        <cfset resultstruct.totalerrors = totalerrors>
        <cfset resultstruct.errorlist   = errorlist>
        <cfif totalerrors GT 0 >
           <cfset resultstruct.success = "no">
        <cfelse>
        </cfif>
     <cfreturn resultstruct>
    </cffunction>

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 37747368
Hi gdemaria.
The original message was "All fields are required.".
If there was an error i did stop the cfloop with a cfabort.
I can do the same here with a cfbreak inside the loop.

Thank you for your help.
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 37747373
Thank you
regards
panos
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 37747477
if you are going to break, then you don't need to count errors, making your code a little simplier..

If you must return a structure...

   <cffunction name="fvalidateCategory" access="public" returntype="any">
     <cfargument name="p_formStructure" type="struct">

      <cfset var formStructure = arguments.p_formStructure>
      <cfset var fieldName = "">
      <cfset var getlanguages = fgetlanguages(ascdesc = "desc",languages_active =1)>
      <cfset var resultStruct = structnew()>
      <cfset resultStruct.success     = "yes">
      <cfset resultStruct.totalerrors = "0">
      <cfset resultStruct.errorlist   = ""> 

      <cfloop query="getlanguages">
          <cfset fieldName = "NewCategory" & getlanguages.languages_id>
          <cfif NOT structKeyExists(formStructure,fieldName)
                 or len(trim(formStructure[fieldName])) eq 0
                 or val(getlanguages.languages_active) eq 0>
              <cfset resultStruct.success     = "no">
              <cfset resultStruct.totalerrors = "1">
              <cfset resultStruct.errorlist   = "All fields are required">
              <cfbreak>
          </cfif>
        </cfloop>
       <cfreturn resultStruct>
   </cffunction>
   

   


Or if you can just return a message...


   <cffunction name="fvalidateCategory" access="public" returntype="any">
      <cfargument name="p_formStructure" type="struct">

      <cfset var formStructure = arguments.p_formStructure>
      <cfset var fieldName = "">
      <cfset var getlanguages = fgetlanguages(ascdesc = "desc",languages_active =1)>

      <cfset var message  = "All fields are required">

      <cfloop query="getlanguages">
          <cfset fieldName = "NewCategory" & getlanguages.languages_id>
          <cfif NOT structKeyExists(formStructure,fieldName)
                 or len(trim(formStructure[fieldName])) eq 0
                 or val(getlanguages.languages_active) eq 0>
              <cfreturn message>
          </cfif>
        </cfloop>
       <cfreturn "">
   </cffunction>

But as an alternative, you could return the fields that failed, in case you want to highlight them or list them..

  <cfset var errorlist = ListAppend(errorlist,fieldName)> <!---- return list of fields ---->
0
 
LVL 2

Author Comment

by:Panos
ID: 37751320
Hi gdemaria
Thank you for your code.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses
Course of the Month6 days, 15 hours left to enroll

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question