Forms-MultipleFields

Only the simplest forms require only a single field to be validated. And, as a user, the most frustrating thing is to have to fix one field before you find out that you had an error in the next field. There are many ways around this problem, but here are a few examples...

As usual, we've left out any explanation of the non-interesting stuff which you can see on Forms-RequiredFields.

See HERE for a working version of this simple form.

Assume a form that looks like this:

|| Name:||(:input text name:) ||
|| Age:||(:input text age:) ||
|| State:||(:input text state:) ||
|| Married||(:input text married:) ||
Name:
Age:
State:
Married

Here is one way to do the validation:

{(wikish_form QUICKFORM PROCESS)}
{(wikish source {$FullName}#validate)}
|| Name:||(:input text name:) ${NameError} ||
|| Age:||(:input text age:) ${AgeError} ||
|| State:||(:input text state:) ${StateError} ||
|| Married||(:input text married:) ${MarriedError} ||
(:input submit save Save:)
(:input end:)

(:if false:)
[[#validate]]
if test -z ${save} # the user didn't press the "save" button
then
   exit
fi
#
# Here's where the interesting stuff starts
#
set errs = 0    # Start assuming no errors
if test -z "${name}"
then
   echo "%red%You must enter your name%%\\"
   set errs ++  # Increment the error count
fi
if ! test "${age}" ~= "/^[0-9]+$/" || test ${age} -lt 18
then
   echo "%red%Your age must be 18 or above.  Ask your mom or dad for help.%%\\"
   set errs ++  # Increment the error count
fi
if ! grep -q "^${state}$" WikiSh.Forms-ListOfStates
then
   echo "%red%Hmmm...  I don't recognize ${state} as a valid state.  Try another.%%\\"
   set errs ++  # Increment the error count
fi
if test "${married}" != "yes" && test "${married}" != "no"
then
   echo "%red%Please enter either 'yes' or 'no' as to your marital status.%%\\"
   set errs ++  # Increment the error count
fi
if test ${errs} -gt 0
then
   echo "%red%Your data was not saved due to the errors mentioned above.  Please fix the problems and save again.%%"
   exit
fi
#
# Here's where the interesting stuff ends
#
# Here I would save the data...
echo "If this were a real form I would have done something productive with this high quality data you have entered."
[[#validateEnd]]
(:ifend:)

That method works fine for relatively short forms where the user can see the entire form plus any error messages on one screen. But if they have to start scrolling up and down to see the error messages and fix them then it can get annoying. So here is another way to solve the problem:

See HERE for a working version of this simple form.

{(wikish_form QUICKFORM PROCESS)}
{(wikish source {$FullName}#validate_init)}
|| Name:||(:input text name:) {(wikish source {$FullName}#validate_name)} ||
|| Age:||(:input text age:) {(wikish source {$FullName}#validate_age)} ||
|| State:||(:input text state:) {(wikish source {$FullName}#validate_state)} ||
|| Married||(:input text married:) {(wikish source {$FullName}#validate_married)} ||
(:input submit save Save:) {(wikish source {$FullName}#validate_final)}
(:input end:)


(:if false:)
[[#validate_init]]
function CheckNeedValidate()
{
   if test -z ${save} # the user didn't press the "save" button
   then
      exit
   fi
}
set errs = 0    # Start assuming no errors
[[#validate_initEnd]]
[[#validate_name]]
CheckNeedValidate
if test -z "${name}"
then
   echo "%red%You must enter your name%%\\"
   set errs ++  # Increment the error count
fi
[[#validate_age]]
CheckNeedValidate
if ! test "${age}" ~= "/^[0-9]+$/" || test ${age} -lt 18
then
   echo "%red%Your age must be 18 or above.  Ask your mom or dad for help.%%\\"
   set errs ++  # Increment the error count
fi
[[#validate_ageEnd]]
[[#validate_state]]
CheckNeedValidate
if ! grep -q "^${state}$" WikiSh.Forms-ListOfStates
then
   echo "%red%Hmmm...  I don't recognize ${state} as a valid state.  Try another.%%\\"
   set errs ++  # Increment the error count
fi
[[#validate_stateEnd]]
[[#validate_married]]
CheckNeedValidate
if test "${married}" != "yes" && test "${married}" != "no"
then
   echo "%red%Please enter either 'yes' or 'no' as to your marital status.%%\\"
   set errs ++  # Increment the error count
fi
[[#validate_marriedEnd]]
[[#validate_final]]
CheckNeedValidate
if test ${errs} -gt 0
then
   echo "%red%Your data was not saved due to the errors mentioned above.  Please fix the problems and save again.%%"
   exit
fi
# Here I would save the data...
echo "If this were a real form I would now be processing the high quality data you have entered."
[[#validate_finalEnd]]
(:ifend:)

(:messages:)

In this second method we have split up the validation into individual field-validation sections and called them immediately after the given field. This gives us the opportunity to see the error messages in the immediate context of the field being entered.

Here's another way to do the same thing, using individual variables to hold the error message for each field:

See HERE for a working version of this simple form.

{(wikish_form QUICKFORM PROCESS)}
{(wikish source {$FullName}#validate)}
|| Name:||(:input text name:) ${NameError} ||
|| Age:||(:input text age:) ${AgeError} ||
|| State:||(:input text state:) ${StateError} ||
|| Married||(:input text married:) ${MarriedError} ||
(:input submit save Save:)
(:input end:)


(:if false:)
[[#validate]]
if test -z ${save} # the user didn't press the "save" button
then
   exit
fi
set errs = 0    # Start assuming no errors
if test -z "${name}"
then
   set -s NameError = "%%You must enter your name%%"
   set errs ++  # Increment the error count
fi
if ! test "${age}" =~ "/^[0-9]+$/" || test ${age} -lt 18
then
   set -s AgeError = "%red%Your age must be 18 or above.  Ask your mom or dad for help.%%"
   set errs ++  # Increment the error count
fi
if ! grep -q "^${state}$" MyGroup.ListOfStates
then
   set -s StateError = "%red%Hmmm...  I don't recognize ${state} as a valid state.  Try another.%%"
   set errs ++  # Increment the error count
fi
if "${married}" != "yes" && "${married}" != "no"
then
   set -s MarriedError = "%red%Please enter either 'yes' or 'no' as to your marital status.%%"
   set errs ++  # Increment the error count
fi
if test ${errs} -gt 0
then
   echo "%red%Your data was not saved due to the errors mentioned above.  Please fix the problems and save again.%%"
   exit
fi
# Here I would save the data...
[[#validateEnd]]
(:ifend:)