Solved

cffunction problem

Posted on 2012-03-21
5
334 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 500 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fixing some Issues with regex again 9 39
cfspreadsheet 15 109
How to do a Left Outer Join sql query in coldfusion 4 108
Combining Queries 7 28
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 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