Solved

cffunction problem

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now