Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts

    Reporting errors on a multi-page form

    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)
    PHP Code:
    //all error messages stored in 1 array using numeric index (0 is not used because 0 means field is valid)
    $errorArray = array (=> "field1 invalid"=> "field2 invalid"=> "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 Code:
    <?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?

  • #2
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    I was having a think about this and perhaps you could put the errors into an array list so..

    PHP Code:
    $_SESSION['field1']['error'][0] = 1;
    $_SESSION['field1']['error'][1] = 2
    Then you could use a loop to display all the errors on a field.

    PHP Code:
    <?php 
    if (isset($_SESSION['field1']['error'])) 
    {
        
        echo 
    "<div class=\"required error\">\n";     

        foreach(
    $_SESSION['field1']['error'] as $error => $value):
            
            echo 
    "<p>{$errorArray[$value]}</p>\n"
        
        endforeach;

       echo 
    "</div>";

    } else { 
        echo 
    "<div class=\"required\">\n"

    ?>
    Something like that i think.

    hope it help any way it just a thought i had.

  • Users who have thanked mic2100 for this post:

    Fumigator (11-06-2007)

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    That is a good way to get around the 1-error-per-field, thanks for that!

    I'm still not in love with the whole process but I'm going to go with it I guess.

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Remember you can add to arrays like this:

    PHP Code:
    $array = array();
    $array[] = "New Element"
    That might make the code cleaner, so you don't have to keep a counter.

    My recommendation is to create a class that does HTML forms. They're so common and repetitive that it is very helpful. It's what I use 99% of the time and it works great. It also makes your forms very consistent which is a nice UI bonus. The only downside currently is that mine does not handle multiple pages, although it is fairly easy to seperate the fields into groups, and only display specific groups to create the effect of pages.

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yes... a form class... by that you mean a class that generates the HTML markup I gather? And contains validation methods? So this class would have a method for each type of form element.. so there's a method to create HTML for a text element, for example, which I would pass the length and element Name to. Then my "html" page would consist of a bunch of formObject->createTextElement("field1", "10"); method calls to generate the form markup, and my validation would be something like $_SESSION['field1']['error'][] = formObject->validateText($_POST['field1'], "required", 1);, where the 2nd argument indicates the type of validation to perform and the 3rd argument indicates the error code if validation fails...? (That's a bit fuzzy)

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Well, there are various methods of complexity you can go for. The thing to remember is that PHP is still a scripting language, one that is not cached by default.

    My current solution is a Form class that accepts objects of type FormField. The basic FormField type implements some validation and HTML generation, but it will not produce anything useful. The power lies in that I can override each of the methods of that class, and create something else. The text field version overrides the output part and inserts an input tag in there. The file upload field does the same, but it also implements the file handling (this requires some event modeling). The output is just the filename, but I told the field where to move the file, what to name it as, etc.

    In the end, every one of my forms is just a list of design settings. No actual code is required. If a project requires a special type of field (for example, a picklist control), I can create that field without touching the code of the main form class.

    But that is just one of many ways.

    One of my first implementations was less object oriented, and just maintained a list of fields with their name, column name (if the form was inserting directly into the database), type, and some various parameters. This proved to be inflexible to where I kept modifying the form for each website, which required retesting.

    EDIT: I do realize there are classes for this out there. But I've never been one to use other people's code. I have a much better understanding of things I wrote myself, and they are far less bloated with extra code.

  • Users who have thanked aedrin for this post:

    Fumigator (11-06-2007)

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I too prefer to write my own stuff and completely understand what the code is doing. It's more work upfront but ultimately saves me work.

    OO is a major weakness of mine so this form class will be interesting for me to see how much OO I can utilize as I progress. I have a feeling my first run will look a lot like the version you mentioned that was inflexible and required changes with each new project... but anything more complex and I think I'd end up homeless drinking 2 dollar wine (making $500/day panhandling of course).

    Anyways thanks for the suggestions!

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    (making $500/day panhandling of course).
    Sign me up!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •