Fumigator
11-05-2007, 11:09 PM
Just thought I'd bounce this off the CF community to see what bounces back. It doesn't quite flow right and I keep thinking there must be a better way to do this.
I'm creating a multi-page form which uses sessions to track data between pages. Each time the user leaves a page (either by submitting or clicking on a link to another page), the validation routine runs and validates all form data.
To track all errors for a given form, I'm thinking about organizing it by storing an error code which corresponds to an error array. I will store the code in the $_SESSION array using the name of the field, like this:
(This is pseudo code, not syntactically accurate)
//all error messages stored in 1 array using numeric index (0 is not used because 0 means field is valid)
$errorArray = array (1 => "field1 invalid", 2 => "field2 invalid", 3 => "field3 invalid");
//validate field1
if (field1 is not valid) {
$_SESSION['field1']['error'] = 1; //code of 1 refers to the index of $errorArray
}
.
.//same for field2...fieldn
.
Then I kick back to the form and modify the form display, adding a CSS class that highlights all fields with validation errors. That's a bit cludgy though, because I have to add PHP to each form element.
This design also restricts the number of errors to 1 per field. If a field has several different errors, I will only be able to display one of them.
<?php
if (isset($_SESSION['field1']['error'])) {
echo "<div class=\"required error\">\n";
echo "<p>{$errorArray[$_SESSION['field1']['error']]}</p>\n";
} else {
echo "<div class=\"required\">\n";
}
?>
<input type="text"> blah blah more html
I will wrap that "if" statement in an include file to tidy up the code, but it still seems kind of cludgy. Any better ideas out there?
I'm creating a multi-page form which uses sessions to track data between pages. Each time the user leaves a page (either by submitting or clicking on a link to another page), the validation routine runs and validates all form data.
To track all errors for a given form, I'm thinking about organizing it by storing an error code which corresponds to an error array. I will store the code in the $_SESSION array using the name of the field, like this:
(This is pseudo code, not syntactically accurate)
//all error messages stored in 1 array using numeric index (0 is not used because 0 means field is valid)
$errorArray = array (1 => "field1 invalid", 2 => "field2 invalid", 3 => "field3 invalid");
//validate field1
if (field1 is not valid) {
$_SESSION['field1']['error'] = 1; //code of 1 refers to the index of $errorArray
}
.
.//same for field2...fieldn
.
Then I kick back to the form and modify the form display, adding a CSS class that highlights all fields with validation errors. That's a bit cludgy though, because I have to add PHP to each form element.
This design also restricts the number of errors to 1 per field. If a field has several different errors, I will only be able to display one of them.
<?php
if (isset($_SESSION['field1']['error'])) {
echo "<div class=\"required error\">\n";
echo "<p>{$errorArray[$_SESSION['field1']['error']]}</p>\n";
} else {
echo "<div class=\"required\">\n";
}
?>
<input type="text"> blah blah more html
I will wrap that "if" statement in an include file to tidy up the code, but it still seems kind of cludgy. Any better ideas out there?