Hey Everybody -
Pretty new to these forums and cold fusion in general. I created a number of
forms in cold fusion for a school. They sign up for classes and when they fill
out ALL text fields they successfully submit the form and they see a success
page. Sounds great right? well kind of.
If a user doesnt fill out the entire form, and submits it gives them an error
with coldfusion errors. So I am faced with a few choices. I can either: have
the form forward them to an error page instead of the coldfusion error, or if
there's a way I can fill in the null values so that there isnt a problem with
nulls. I want to have validation but I want it in a seperate page since I dont
have the room to add errors in the form itself.
Any help would be great. Thanks in advance.
Oguz.Demirkapi - 20 Mar 2008 20:15 GMT
You need to validate your form fields either in server side via CF or in client
side via JavaScript etc.
Here is the official documentation:
http://livedocs.adobe.com/coldfusion/7/htmldocs/00001381.htm
There is also a nice tutorial about form usage.
http://www.adobe.com/devnet/coldfusion/articles/richforms_02.html
I also would suggest to check jQuery client side form validation in the
following link.
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Azadi - 21 Mar 2008 02:37 GMT
> You need to validate your form fields either in server side via CF or in client
> side via JavaScript etc.
it is not either / or. you should ALWAYS validate form data on the
server. you can also use client-side validation to make it more
user-friendly, but never rely on it to validate your data. validate on
the server. for one, if a client has js turned off in their browser your
client-side validation flies out the window...
i have not checked the links Oguz provided, but basic concept of form
validation is:
<cfparam> your form vars on the action page to default values - that way
if a user does not fill in a field, it will default to the value you set
in cfparam for it.
text fields ALWAYS have a value - an empty string '' if it has not been
filled in. checkboxes and radio button, if not checked, are not
submitted with the form. so cfparam those always.
drop-down select lists are treated differently by different browsers.
some preselect the first element in the list, others set the value of
the select to -1 if nothing is selected. you can control default
selection in the list with seslected="selected" attribute in the field.
it is useful to cfparam these as well...
if you want the user to be able to try submitting the form, show errors
on next page, then return to the form and have their previously entered
data pre-filled for them, use session-scope vars to store form data:
just copy your form to session var on your action page. make sure
sessions are enabled in cf administrator and in your application.cfm/cfc
hth
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Oguz.Demirkapi - 21 Mar 2008 03:25 GMT
I think it was a wrong expression. :)
You have to validate always in server side. But client side validation is just good for usability and not required for validation process.
BKBK - 21 Mar 2008 07:41 GMT
Use
http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhel
p.htm?context=ColdFusion_Documentation&file=00000256.htm. Typical fields like
http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhel
p.htm?context=ColdFusion_Documentation&file=00000279.htm,
http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhel
p.htm?context=ColdFusion_Documentation&file=00000331.htm and
http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhel
p.htm?context=ColdFusion_Documentation&file=00000341.htm have attributes that
enable you to get Coldfusion to do your clientside validation for you. Here
follows a rough sample to whet your appetite:
<cfif isDefined("form.sbmt")>
Form successfully submitted.
<cfdump var="#form#"><cfabort>
</cfif>
<cfform action="#cgi.script_name#" method="post">
User name: <cfinput type="text" label="user name:" name="userlogin"
required="yes" message="Login name required" value="a"><br>
Password: <cfinput type="password" name="password" required="yes"
message="Password required" value="s"><br>
e-mail: <cfinput type="text" name="email" required="yes" message="You must
supply a valid e-mail address" validate = "email" value="ac@def.com"><br>
Date of birth: <cfinput type = "text" required="yes" name = "dob" message =
"You must enter a valid date in any of the formats yyyy-mm-dd, mm/dd/yyyy or
mm/dd/yy" validate = "date"><br><br>
<cfinput type="submit" name="sbmt" value="Log in">
</cfform>
Dan Bracuk - 21 Mar 2008 13:16 GMT
Before you do any of that, make sure you understand the requirements of the app you are writing. Specifically, determine whether or not it is necessary to fill in every text box.
BKBK - 21 Mar 2008 17:12 GMT
Dan Bracuk wrote:
[i]Before you do any of that, make sure you understand the requirements of the
app you are writing. Specifically, determine whether or not it is necessary to
fill in every text box.[/i]
I assumed that that is implicit, as Tec28 says, [i]"when they fill out ALL
text fields they successfully submit the form"[/i].
J.C. - 26 Mar 2008 15:05 GMT
I'm wondering, if by chance, that you have a checkbox or radio button on your
submit page. If so, this may be why it's blowing up. If they don't have a
response, the target page won't see the form. You have to cfparam those...
<cfparam name="form.radioButtonA" default="0" type="any">
AlwaysWannaLearn - 26 Mar 2008 17:34 GMT
I always do ALL my form validations with a client-side JS **and** with
server-side CF.
I don't use <cfform>, I know <cfform> is dependant on the java version the
user has installed on their end. I know I came across quite a few issues
where on one machine the <cfform> worked fine, and on another it konked out.
Anways, this is one of my most basic forms:
=============================================
[b]PAGE1.cfm[/b]
<html>
<head>
<script>
function validate() {
var IA = document.ContactUs;
var errorMSG = "";
if(IA.FullName.value == "") {errorMSG += "Full Name\n";}
if(IA.Email.value == "") {errorMSG += "Email\n";}
if(errorMSG != "") {
alert("The following fields are required:\n\n" + errorMSG);
return false;
}
}
</script>
</head>
<body>
<form name="ContactUs" action="page1_x.cfm" method="post" onSubmit="return
validate();">
<table>
<tr>
<td align="right">Name: </td>
<td align="left"><input type="text" name="FullName" maxlength="50"
size="25"></td>
</tr>
<tr>
<td align="right">Email Address: </td>
<td align="left"><input type="text" name="Email" maxlength="50"
size="25"></td>
</tr>
<tr>
<td align="right">Comments: </td>
<td align="left"><textarea name="Comments" cols="20" rows="5"
id="comments"></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="SubmitContact" value="Submit">
<input type="reset" name="Reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
=============================================
=============================================
[b]page1_x.cfm[/b]
<cfif isdefined("FORM.SubmitContact") and FORM.FullName NEQ "" and FORM.Email
NEQ "">
<!--- is all is OK, do whatever it is you want to do. --->
<cfelse>
<!--- if the submit button was no clicked, fullname and email (required
fields) not filled out, send them back to the form --->
<cflocation url="page1.cfm?status=1" addtoken="no">
</cfif>
=============================================
Now, there is sooooo much more you can here for a full blown error-proof
script. You can also check if the e-mail is a valid e-mail (server side), and
if the field is a numeric field (like zip code, phone number, etc) then check
if that field is valid, etc.
This is just a run-of-mill simple form and validation submission using both
client-side JS and server-side code.