I am fairly new to coldfusion. so this may be a basic question. I am working on a form where i want to make some of the fields only required if a check box below the fields is left blank. If the check box has a check in it, then the above fields wouldn't be required.

<div align="center" class="style22 style21">Ground Transportation</div><br />

<table width="610" height="5" align="center">
    <td width="299"><div align="center">Car Type<br />
       <CFSELECT NAME="car_type">
         <option value="None selected">Select One </option>
         <option value="Full Size">Full Size</option>
         <option value="Intermediate">Intermediate</option>
         <option value="Compact">Compact</option>
         <option value="Sub-Compact">Sub-Compact</option>
         <option value="SUV">SUV </option>
         <option value="Mini-Van">Mini-Van</option>
         </CFSELECT>
    </div></td>
          
          <td width="299"><div align="center">Two/Four-Door<br /> 
            <CFSELECT NAME="num_car_doors"> 
              <option value="None selected">Select One </option>
              <option value="Two Door">Two Door </option>
              <option value="Four Door">Four Door</option>
              </CFSELECT>
          </div></td>     
    
</table>

<table width="610" align="center"><div>
      <td width="170">Car Company<cfinput type="text" width="160" name="car_co1"></td>
	  <td width="176">Corporate Discount Num
	    <cfinput type="text" width="160" name="car_disc1"></td>
      <td width="170">Express Service ID<cfinput type="text" width="160" name="car_id1"></td>
  </table><br />
  
  
  <table width="610" align="center">
    <tr>
      <td width="610">
           <cfinput type="checkbox" NAME="car_auth" >
        No car arrangements required.</td>
    </tr>
  </table>

So basically, if the user checks the box for no car arrangements required, then the above fields wouldn't require validation. But if he leaves the field no car arrangements required, then the above fields would be mandatory.

is this possible in cold fusion? I'm not asking anyone to spoon feed me the code..just point me in the right direction. Would i need an cfif statement of some kind?

any help would be appreciated.

Checkboxes are a funny thing in form posts. If they aren't checked then they don't exist in the form scope that the page posts to. Also, if you don't specify a value for the checkbox, and the user clicks it, then the value of the checkbox in the form scope on the action page is "on". Here is a bit of code to make sure that the checkbox always returns a 1 or 0 value.

<cfparam name="FORM.car_auth" default="0">
<cfif FORM. EQ "on">
    <cfset FORM.car_auth = 1>
</cfif>

As for making sure that the other values are populated, yes you should use a cfif statement to see if the other fields need validation. Then for every field that you need validated, check if the field is defined and the value isn't an empty string (again using the cfif statement). If this conditional is true, then display validation information and a cfabort if appropriate.

If you want to stretch your brain a little (and get some cfml experience in the process), you could also make a short snippet of code that defines a comma delimited list of keys for the FORM scope. Then loop over each of the keys and use the conditional described above. I use an enhanced version of the snippet that I just described for setting up quick validation of form inputs. Instead of spending 10 minutes setting up form validation for each individual input, I use a generic snippet of code and am done and working on something else in less than a minute.

NOTE: I personally can't stand using the coldfusion provided form tags. In my opinion they are utterly horrible and are something to be avoided. But to each their own. I have seen really nice code using them.

Thanks for all your help! putting the cfparam tag in the processing page helped get rid of the error message about the check box being undefined.

I have another question though. I'm working on a separate form, a timeoff request form for our intranet. I'm actually converting an old form that was written in PHP to coldfusion. Anyway, there is a drop down box that contains a list of all the supervisor's names. The end user fills out the form and they select their supervisor's name. So the Coldfusion form uses the cfselect name="supervisor". I'm trying to make it so that whatever supervisor is chosen from the drop down menu the supervisor will get a copy of the email.

In my processing form, i have:

<CFMAIL 
		from="#form.email#" 
		to="#form.email#, #form.supervisor#" 
		type="html" 
		subject="Time Off Request">

However, the processing form doesn't seem to like it when i use the variable from the cfselect drop down. If i take out #form.supervisor#, then everything works fine.

Is it possible to call upon the cfselect variable under the cfmail tag?

in fact when i go through the log files i see this error message "attribute validation error for tag cfmail"

nevermind...it's working now. i had a mistake in the code..accidentally had a wrong email address in there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.