I have a form with dynamic checkboxes and radio buttons. They output from my db 
tables that have tblanswersets (one) and tblanswers (many) 

I'm grouping some questions, then spitting out these items. It mostly works, 
except the checkboxes. Right now, my server side validation makes sure ANY 
group of checkboxes or radio buttons have at least one selection. When the form 
submits to itself, the values are saved and form items are pre-checked/selected 
if something's caught in the validation. 

But my checkboxes get caught if there isn't a selection. I've decided to add a 
field to tblanswersets - answersetrequired  = yes/no

I can output that somehow in my form inputs to setup some new validation 
routine. Thought you guys might have a good way to do this. I'll show you what 
I'm using now, maybe gimme some pointers/ideas on how to improve this? 

Thanks,
Will

<!--- Get CORE questions for this eval--->
<cfquery datasource="#SESSION.DSN#" name="getCoreQuestions">
SELECT
tblquestionsets.questionsetid,
tblquestionsets.questionsetname,
tblquestions.questionid,
tblquestions.answersetid,
tblquestions.questionsetid,
tblquestions.question,
tblquestions.questioncatid,
tblquestions.questionsortnum,
tblquestioncats.questioncatid,
tblquestioncats.questioncatname,
tblanswersets.answersetid,
tblanswersets.answersetname,
tblanswersets.answersettypeid,
tblanswersets.answersetrequired,
tblanswersettypes.answersettypeid,
tblanswersettypes.answersettypename,
tblanswers.answerid,
tblanswers.answername,
tblanswers.answervalue,
tblanswers.answersortnum,
tblanswers.answersetid,
tblquestionsets.questionsetdateadded
FROM
tblquestionsets,
tblquestions,
tblanswersets,
tblanswersettypes,
tblanswers,
tblquestioncats
WHERE
tblquestionsets.questionsetID = tblquestions.questionsetID
AND tblquestions.answersetID = tblAnswersets.answersetid
AND  tblanswersets.answersetid = tblanswers.answersetID
<!--- This will be dynamic in the future. Hard-coded for testing for now --->
AND tblquestionsets.questionsetID = 1
<!--- We need to just pull CORE questions in this query --->
AND tblquestions.questioncatid = 1
AND tblanswersets.answersettypeid = tblanswersettypes.answersettypeid
AND tblquestions.questioncatid = tblquestioncats.questioncatid

ORDER BY tblquestions.questionsortnum, tblanswers.answersortnum
</cfquery>


<!--- Output CORE question query here --->
        <cfoutput query="getCoreQuestions" group="questionID">
        
        <cfset ctr = ctr + 1>
      <tr class="background-hilite">
        <td align="center" width="75">
                #ctr#
                 </td>
        <td>#question#</td>             
                <td style="text-align:center;"><cfif VARIABLES["Q_question" & 
ctr & "flagged"] EQ true><img src="../images/warningsymbol.gif" />
                 </cfif>
                 </td>
                                
        <td width="200">
                <table align="center" width="100%"  border="0" cellspacing="0" 
cellpadding="8">
            <tr class="background-hilite">
                        
                        <!--- Output answers here --->
                        <cfoutput>
                        <td align="center" width="25%">#answervalue#
                        <br />
                        
                        <cfif VARIABLES["fieldvalue" & ctr] EQ 
"#questionID#_#answerID#">
                        <cfset buttonChecked = "yes">
                        <cfelse>
                        <cfset buttonChecked = "no">
                        </cfif>
                        
                        <cfinput name="Q_question#ctr#" 
type="#answersettypename#" value="#questionID#_#answerID#" 
checked="#buttonChecked#" />
                
                        </td>
                        </cfoutput>
            
            </tr>               
          </table>
                  </td>
      </tr>
          </cfoutput>    
          <table width="100%" border="0" cellspacing="0" cellpadding="5" 
summary="x">
                 <tr class="background-hilite">
        <th colspan="6"><h2>Additional Questions</h2></th>
         </tr>

Then I param the formfields:

<!--- Param our formfields --->
<cfoutput query="getCoreQuestions" group="questionID">
        <cfset selectedctr = selectedctr + 1>
        <cfoutput>
                 <cfparam name="FORM.Q_question#selectedctr#" default="">
                 <cfparam name="VARIABLES['Q_question' & selectedCtr & 
'selected']" default="no">
                 <cfparam name="VARIABLES['fieldvalue' & selectedCtr]" 
default="">
                 <cfparam name="VARIABLES['Q_question' & selectedCtr & 
'flagged']" default="false">
                </cfoutput>
</cfoutput>

Here's where I setup an errorstruct

<!--- Start validating data --->
 <cfset errorStruct = StructNew()>
        <cfloop from="1" to="#FORM.numQuestions#" index="thisNum">      

        <!--- If the value of this answer is blank, create an error --->
         <cfif NOT Len(FORM["Q_question" & thisNum])>
                   <cfset errorStruct.errormessages[thisNum] = "Please make a 
selection for question #thisNum#">
                                   
                    <cfset VARIABLES["Q_question" & thisNum & "flagged"] = true>
                    <cfelse>
                    <cfset VARIABLES["fieldvalue" & thisNum] = 
FORM["Q_question" & thisNum]>
                        
                                                   
                  <cfset VARIABLES["error" & thisNum] = "Please make a 
selection for question #thisNum#">
             </cfif>
        </cfloop>

If my code contains items in the error struct, I show a warning:

<cfif VARIABLES["Q_question" & ctr & "flagged"] EQ true><img 
src="../images/warningsymbol.gif" />
</cfif>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Upgrade to Adobe ColdFusion MX7
The most significant release in over 10 years. Upgrade & see new features.
http://www.adobe.com/products/coldfusion?sdid=RVJR

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:274352
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to