Reputation: 263
In a form, there are radio buttons for the user to select:
<li>
<label for="full professor"><span class="required">&##8727;</span>Is the candidate a full professor?</label>
<input type="radio" name="fullProfessor" value="yes" tabindex="4" />Yes<input type="radio" name="fullProfessor" value="no" tabindex="5" checked/>No </li>
<li>
<label for="more than ten"><span class="required">&##8727;</span>Has the candidate been here for more than 10 years?</label>
<input type="radio" name="moreThanTen" value="yes" tabindex="6" />Yes<input type="radio" name="moreThanTen" value="no" tabindex="7" checked/>No </li>
<li>
<label for="past nominee"><span class="required">&##8727;</span>Has the candidate been nominated for this award in the last 3 years?</label>
<input type="radio" name="pastNominee" value="yes" tabindex="8" />Yes<input type="radio" name="pastNominee" value="no" tabindex="9" checked/>No </li>
I'm not certain how to param them. The typical code I use is:
<cfparam
name="FORM.fullProfessor"
type="string"
default=""
/>
Is the type string even though it doesn't accept a string like a textfield would?
To validate the radio button, is using:
<!--- Validate campus address. --->
<cfif NOT Len( FORM.fullProfessor)>
<cfset ArrayAppend(
arrErrors,
"Error message here."
) />
</cfif>
Lastly, maintaining the users' selection after validating the page, how may I ensure this?
<cfif isDefined(form.fullProfessor) selected="yes">?
Thanks.
Upvotes: 1
Views: 2004
Reputation: 13827
use structkeyexists of existing CF functions
<input type="radio" name="fullProfessor" value="yes" tabindex="4" <cfif structkeyexists(form.fullProfessor) AND form.fullProfessor EQ "true">checked</cfif> />
Upvotes: 0
Reputation: 28873
Is the type string even though it doesn't accept a string like a textfield would?
The type
is what sort of value are you expecting FORM.fullProfessor
to contain (a date, number, string, etcetera). Since your radio button values are "yes" or "no", you could technically use either type="string"
or type="boolean"
.
maintaining the users' selection after validating the page
Since you are already using cfform, you could use <cfform preserveData="yes" ..>
. (Note: The form must be submitted to the same page). It will preserve the state of most form fields. Alternately, you could take advantage of the fact that your radio button values are boolean, and use it to set the button's checked
state.
<cfparam name="FORM.fullProfessor" type="boolean" default="no" />
<cfform>
<cfinput type="radio" name="fullProfessor" checked="#FORM.fullProfessor#" value="Yes"> yes
<cfinput type="radio" name="fullProfessor" checked="#not FORM.fullProfessor#" value="no"> no
<input type="submit">
</cfform>
To validate the radio button
I am not sure how useful validation would be here. You could just set the default to something reasonable like no
and leave it at that. Depends on the form.
Upvotes: 0
Reputation: 3443
If you are using Yes and No you can treat them like booleans.
<cfparam
name="FORM.fullProfessor"
type="string"
default=false
/>
and
<cfif NOT FORM.fullProfessor>
<cfset ArrayAppend(
arrErrors,
"Error message here."
) />
</cfif>
and
<cfif form.fullProfessor selected="yes">
You could also use a framework for validating your forms, I'd highly recommend validate this: http://www.validatethis.org/
Upvotes: 1