Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts

    dynamic FORM fieldname validation!!

    Hi Guys,

    is there a way to ask if the #FORM.fieldname# exist to proceed with the next one ?

    FYI : StartDateName22 is a dynamic generated textbox

    i am using the following code :

    <cfif not isDefined (#FORM.StartDateName22#) or #FORM.StartDateName22# or EQ "" >
    <cfoutput>
    Elemnt 2,2 is not defined
    </cfoutput>
    <cfelse>
    <cfoutput>
    Element 2,2 is defined asc: #FORM.StartDateName22#
    </cfoutput>
    </cfif>

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    What do you mean by "proceed with the next one"?

    You are doing the <cfif> somewhat correctly (you don't need the # signs in a <cfif>, and you have too many "or"s). Do you have this code in a loop or something, and want to check all fields dynamically?

    You could do something like this:
    Code:
    <cfloop from="1" to="50" index="i">
      <cfif NOT structKeyExists( form, 'startDateName' + i ) OR form[ 'startDateName' + i ] EQ "">
        <cfoutput>Form element #i# is not defined or empty</cfoutput>
      <cfelse>
        <cfoutput>Form element #i# is defined as: #form[ 'startDateName' + i ]#</cfoutput>
      </cfif>
    </cfif>
    Or if this code is supposed to handle a grid of form fields, you could have a nested loop inside. The outer loop does rows, the inner one does columns (or however you want to do it).

  • #3
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Hi Gjslick,
    i am having everything working except the Checkbox that does not give the value expected .

    below is my code

    <cfif IsDefined (#FORM["Status#i#11"]#)>
    #FORM["Status#i#11"]#
    <cfelse>
    Off
    </cfif>


    I am trying to show the value and the wiered thing is when i check or uncheck it the value is always "on" :


    Also if it is unchecked when i submit the value i got :

    Element STATUS111 is undefined in FORM.

    And when It is checked and submit the value i got :
    'Off'


    any help ias i am getting lost here..

    thanks

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    Yikes, does ColdFusion even process that <cfif> with that many # symbols??


    First, I do have one recommendation for this. I think you have a grid of form fields, and you are basically naming your variables as: status[row][col] right? If this is the case, it would explain the "weird" behavior of how your form fields are getting submitted.

    Basically, there's a problem in naming your fields like that, in which form field names can easily "conflict" by getting the same name. For example:
    Row 1, Col 11 = status111
    Row 11, Col 1 = status111

    I'd recommend putting an underscore (or something else) between them. Ex:
    status[row]_[col]. This will make it so your form fields come out as this, which will make them each have a unique name:
    Row 1, Col 11 = status1_11
    Row 11, Col 1 = status11_1

    --------------------------------------------------------

    That being said, checkbox fields themselves are interesting in how they get submitted. If they are checked, they are submitted with the value "on" (or whatever value you have defined on them). If they are not checked, they don't get submitted at all, which means they will be undefined.

    So you should be able to do this:
    Code:
    <cfif isDefined( form[ "status#i#11" ] )>
      #form[ "status#i#11" ]#
    <cfelse>
      (field was not checked)
    </cfif>
    I'm not 100% sure that CF7 will process nested # symbols though. It seems to be working on yours, but just in case it isn't:
    Code:
    <cfif isDefined( form[ "status#i#11" ] )>
      #form[ "status" & i & "11" ]#
    <cfelse>
      (field was not checked)
    </cfif>

    --------------------------------------------------------


    Btw, I just realized that the code I gave you last time needed the & operator instead of the + operator. Here is the same code that should (in theory) work:
    Code:
    <cfloop from="1" to="50" index="i">
      <cfif NOT structKeyExists( form, 'startDateName' & i ) OR form[ 'startDateName' & i ] EQ "">
        <cfoutput>Form element #i# is not defined or empty</cfoutput>
      <cfelse>
        <cfoutput>Form element #i# is defined as: #form[ 'startDateName' & i ]#</cfoutput>
      </cfif>
    </cfif>

    --------------------------------------------------------

    Hope that helps. Otherwise, I might need to see your page in action if you have it up on the internet somewhere. Lemme know.

    -Greg

  • Users who have thanked Gjslick for this post:

    korssane (08-21-2011)

  • #5
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Hi Gjslick,

    i got it working thanks/.

  • #6
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for updating regarding validation


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •