Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cffunction problem

Posted on 2012-03-21
5
Medium Priority
?
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

715 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