Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Form vaidation question

Community Beginner ,
Dec 05, 2008 Dec 05, 2008
This is suppose to be a simple task and I have some idea of how to do it but I'm not sure if my way is the best way.
I have a form and many fields are required fields. The previous programmer had applied the client side form validation using javascript but did not apply server side validation.
Many of our users don't have their browser javascript enabled so we have some problem with our data in our DB.

I need to work on server validation but I don't know if there is a better technique out there especially for situation where user has filled out most of the fields but left one or two required field blank.
Once the form is submitted server side validation is doing its work and throw a message back to user to filled out the 2 required fields but still keep the information on other fields filled out previously.

What can I do to the already filled out form fields to keep what user already entered? this way user doesn't have to re-type the whole thing from start.
If I'm the user I will be furious if I have to start from scratch again.

Do I have to do insert first, then get the data to populate all the fields but show message to user those empty one need to be fill out and once they do that I do an update?
it sound to much work on the server side if I have to do it this way, am I right?




TOPICS
Getting started
522
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2008 Dec 05, 2008
You can copy your form variables to session variables to repopulate the form.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 05, 2008 Dec 05, 2008
Ah yes! I can do that. Thank you Dan!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 05, 2008 Dec 05, 2008
Hi,

I would recommend against session variables. That has caused a lot of problems for me in the past. Especially if users have multiple browser windows open, because you have transfer of the data from one window to the other.

There's two things I would recommend:
Do not use the standard edit-page / action page structure.
Instead use a form, that posts all data to itself by default and all edit field should be setup to take the form values to pre-populate the form.
Then at the beginning of that script verify if the "save" button was clicked, and in that case save the data to the database, and leave the form to the next screen using cflocation.

I have copied some lines as an example.
But it's probably is too late to change your existing application

cheers,
fober


<cfif isdefined('btn_save')>
<cfinclude ...some script to save the data
</cfif>

<cfoutput>
<form method="post">
#imform_edit('fname','First name')#
#imform_edit('lname','Last name')#
#imform_edit('company','Company')#
#imform_button('btn_save','Save')#
</form>
</cfoutput>



<cffunction name="imform_edit" output="Yes">
<cfargument name="name" type="string" default="">
<cfargument name="title" type="string" default="">
<cfargument name="required" type="string" default="false">
<cfargument name="style" type="string" default="">
<cfargument name="format" type="string" default="">
<cfargument name="readonly" type="string" default="false">

<cfparam name="temp_value" default="">

<cfparam name="form.#name#" default="">
<cfset temp_value= form[name]>

<label for="#name#">#title#<cfif required is true>*</cfif></label>
<input type="Text" name="#name#" id="#name#" value="#temp_value#"
<cfif isdefined('btn_save') and temp_value is "">style="border:1px solid red"</cfif>><br>

</cffunction>

<cffunction name="imform_button" output="Yes">
<cfargument name="name" type="string" default="">
<cfargument name="title" type="string" default="">

<input type="Submit" name="#name#" value="#title#"><br>

</cffunction>



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 07, 2008 Dec 07, 2008
LATEST
Alecken wrote:
Do I have to do insert first, then get the data to populate all the fields but show message to user those empty one need to be fill out and once they do that I do an update? it sound to much work on the server side if I have to do it this way, am I right?

Right. A trip to the database is a CPU-intensive server operation which you wont want to do often. There are better ways.

One of the best ways is also the most obvious. There is no need to worry about storage. After submission, Coldfusion holds each form field in memory as form.aField. Copy its value back into the respective field, as in this example:

<!--- form has been submitted and form fields validated --->
<form>
<input type="text" name="username" size="20" value="#form.username#">
<form>

You can also use cfform's preserveData attribute.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources