Skip to main content
Inspiring
October 27, 2008
Question

Validating same field names

  • October 27, 2008
  • 4 replies
  • 601 views
I am dynamically generating cfinputs inside a cfloop, so they all have the same field names. Becuase of this, I cannot use the cfinput required attribute to validate. If I use fieldname#i#, the cfinput validation works, but then my insert will not work.

How can I validate input fields that have the same name ?
    This topic has been closed for replies.

    4 replies

    Inspiring
    October 31, 2008
    Hi,

    Have a look at the following part of my script:
    <cfset i=1>
    <cfset valx="">
    <cfloop condition = "isDefined('gfmPartNumberID_#i#')">
    <cfset x= form["gfmPartNumberID_#i#"]>
    <cfset valx= listappend(valx,x)>
    <cfset i= i+1>
    </cfloop>

    This is already an example of how to access all the different fields.
    I'm using "<cfset x= form["gfmPartNumberID_#i#"]>" to get each field value from the "form" structure. In my example I'm just adding them to the list valx, but in your case you can also use that to do the DB updates.

    Also have a look at the cfloop with condition. You need that because you may not know how many fields actually got submitted.

    cheers,
    fober
    Inspiring
    October 27, 2008
    Hi,

    You need to name the fields independently for the validation to work.
    But you can combine all these values into one field once the form has been submitted.

    cheers,
    fober
    =================================================

    <cfoutput>
    <TABLE border="0" width="760">
    <cfform>
    <TR>
    <TD class="TitleText">
    <B>Material Number:</B>

    <TABLE border="0" width="300">
    <CFLOOP from="1" to="10" index="i">
    <TR>
    <TD align="left" class="TitleText" width="140">
    <B>Serial Number #i#:</B> 
    </TD>
    <TD align="left" width="160">
    <CFINPUT type="text"
    name="gfmPartNumberID_#i#" required="Yes" message="Serial Number for #i# must be entered.">
    </TD>
    </TR>
    </CFLOOP>
    </TABLE>
    <input type="Submit" name="" value=">>">
    </TD>
    </TR>
    </cfform>
    </TABLE>
    </cfoutput>

    <cfset i=1>
    <cfset valx="">
    <cfloop condition = "isDefined('gfmPartNumberID_#i#')">
    <cfset x= form["gfmPartNumberID_#i#"]>
    <cfset valx= listappend(valx,x)>
    <cfset i= i+1>
    </cfloop>

    <cfoutput>
    @(#valx#)
    </cfoutput>
    trojnfnAuthor
    Inspiring
    October 30, 2008
    Hello fober1,

    Thanks for your response.

    I took your sample advice and now the cfinput validation works, with each name being different now with the addition of #i# at the end. The error message now displays.

    In the action page, I put in cfoutput to display the data, and the last one, #valx# displays a list of the serial numbers entered. So I setup my cfloop like this :

    <cfloop list="#valx#" index="i">

    Now my question is how do I define each serialNumber#i# so that each can be inserted into the table ?
    I am totally lost on this aspect.

    Thanks for your help.
    Inspiring
    October 27, 2008
    You would be better off going with the different input names and figuring it out on the results page.

    I'm not positive, but I'm pretty sure we've taught you how to do that at least once.
    Inspiring
    October 27, 2008
    the value of the same-name fields is a comma-delimited list of values
    from individual fields. so if you have several text input fields named
    "mytext", then you get #form.mytext# with a comma-delimited list of values.

    how to validate it depends on what you want to validate for:
    a) all fields required: listlen(trim(form.mytext)) eq
    total_number_of_mytext_fields
    b) X of fields required: listlen(trim(form.mytext)) eq X
    c) at least one filed required: listlen(trim(form.mytext)) gte 1
    d) to validate data format: loop through form.mytext list and validate
    each list element.

    hth

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/