Solved

cffunction problem

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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

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 …
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 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