user4409149
user4409149

Reputation:

Coldfusion Server Side Validation Sometimes Required sometimes not depending on radio buttons

I am trying to get a better understanding of server side validation. I have been writing a lot of client side validation using JavaScript but did not realize if the user just turns JavaScript off the whole app does not validate any field. This is what I am looking into doing for ColdFusion Server side validation.

What I am wondering is how would you toggle on and off making a field required or not based on lets say a radio buttons yes or no. Say you have a radio button the if yes is chosen it makes another input required but if no is chosen it makes another field required. I was just wondering how you would do something like that with the _cf format of toggling on and off. Would you just create if statements on hidden fields to do so or something?

I have been researching it a lot and just was looking for some input on how people are achieving things like this because it just seems like you can do so much through client side but then its all pointless because they can just turn it off.

<table>
<tr>
    <td>Date:</td>
    <td><input type="text" name="date" size="20"></td>
    <strong class="delete me and add my line number to the highlight in my pre tag">
    <input type="hidden" name="date_cfformrequired" value="You must enter a date.">
    <input type="hidden" name="date_cfformdate" value="The date you entered is not a valid date."></strong>
</tr>
<tr>
    <td>Distance:</td>
    <td><input type="text" name="distance" size="20"></td>
    <strong class="delete me and add my line number to the highlight in my pre tag">
    <input type="hidden" name="distance_cfformrequired" value="You must enter a distance.">
    <input type="hidden" name="distance_cfformfloat" value="The distance you entered is not a valid number."></strong>
</tr>
<tr>
    <td>Time:</td>
    <td><input type="text" name="time" size="20"></td>
    <strong class="delete me and add my line number to the highlight in my pre tag">
    <input type="hidden" name="time_cfformrequired" value="You must enter a time.">
    <input type="hidden" name="time_cfformregex" value="^\d{1,3}:\d{0,2}$"></strong>
</tr>
<tr>
    <td>Comments:</td>
    <td><input type="text" name="comments" size="50"></td>
    <strong class="delete me and add my line number to the highlight in my pre tag">
    <input type="hidden" name="comments_cfformrequired" value="You must enter a comment.">
    <input type="hidden" name="comments_cfformmaxlength" value="50"></strong>
</tr>
<tr>
    <td colspan="2" align="right">
        <input type="submit" name="Add Entry">
    </td>
</tr>
</table>

Upvotes: 1

Views: 351

Answers (1)

Regular Jo
Regular Jo

Reputation: 5510

Your understanding of the difference between client and server side validation is correct. Consider that client side validation can be chalked up to a feature. Rather than the user entering information, submitting the form, and then being told something was incorrect, Javascript validation can guide them, but because it can be disabled, it's not good to rely on.

Server side validation always happens as dictated.

The code is pretty easy

As an example - HTML

Name:
<input type="text" name="myname">

Were you referred by a current user?
<input type="radio" name="Referred" value="0" checked> No
<input type="radio" name="Referred" value="1"> Yes

Who referred you?
<input type="text" name="ref_user">

Form processing

<cfset Err = {Messages = []}> // creates a struct named Err with an array names Messages.

<cfif len(trim(form.myname)) eq 0>
  <cfset ArrayAppend(Err.Messages,"Please enter your name!">
</cfif>

<cfif form.Referred eq 1 and len(trim(form.ref_user)) eq 0>
  <cfset ArrayAppend(Err.Messages,"You said someone referred you, who was it?">
</cfif>

<cfif ArrayLen(err.messages) eq 0>
  ...no errors, we can do form processing...
</cfif>

And then, when outputting your errors, if they exist

<cfif ArrayLen(err.messages) gt 0>
    Sorry, an error occurred.<br>
    <cfoutput>
    <cfloop array="#err.messages#" index="cError">
      - #cError#.<br>
    </cfloop>
    </cfoutput>
</cfif>

On the first line of the last code snippet, you'll see gt 0 at the end of the line. This is not necessary and most people will leave it off. You seem familiar with javascript, cf works the same way in that in that you might say

if (Err.Messages.length) {
  ... show
}

In cf, you can do similar, <cfif ArrayLen(Err.Messages)>.

Remember to either cfparam your variables or check for their existence in your form processing, like this..

<cfif not StructKeyExists(form, "Referred") or (form.Referred eq 1 and len(trim(form.ref_user)) eq 0)>
  <cfset ArrayAppend(Err.Messages,"You said someone referred you, who was it?">
</cfif>

Upvotes: 3

Related Questions