How should I go about validating a large form that has a "grid" of input fields and check boxes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gleep

    How should I go about validating a large form that has a "grid" of input fields and check boxes?

    Hi PHP coders,
    I've got an issue I'm stuck with. Imagine there is a large form that has 5 columns and 20 rows.
    In each row there is a check box - then 4 input fields. I already have the code that inserts all
    the data BUT the validation is a nightmare.

    What I need is. If an entire row is empty (not checked or filled out) that's OK. However if a user
    fills in one or two input fields within a row - that's NOT ok. The entire row needs to be filled
    out. It's an all or nothing situation.

    Lets say each column is called a, b, c, d, e
    I started writting various if statements but it instanly becomes out of control.
    example if a but not b and a and b and not c etc.... each row has many combinations.

    How can i structure php code that looks at each row and determines if the row is partically filled
    out, then valdiation fails . Validation passes if the row is complely empty or completely full.

    Heres the kicker, the input fields in this form do not have a consistant naming pattern all the
    fields have unique identifier. Also one column expect a date another an integer another text.

    Thanks for your help
  • Pedro Graca

    #2
    Re: How should I go about validating a large form that has a "grid&quot ; of input fields and check boxes?

    Gleep wrote:[color=blue]
    > I've got an issue I'm stuck with. Imagine there is a large form that
    > has 5 columns and 20 rows.
    >
    > In each row there is a check box - then 4 input fields. I already
    > have the code that inserts all the data BUT the validation is a
    > nightmare.
    >
    > What I need is. If an entire row is empty (not checked or filled out)
    > that's OK. However if a user fills in one or two input fields within
    > a row - that's NOT ok. The entire row needs to be filled out. It's
    > an all or nothing situation.
    >
    > Lets say each column is called a, b, c, d, e
    > I started writting various if statements but it instanly becomes out
    > of control.
    > example if a but not b and a and b and not c etc.... each row has
    > many combinations.
    >
    > How can i structure php code that looks at each row and determines if
    > the row is partically filled out, then valdiation fails . Validation
    > passes if the row is complely empty or completely full.[/color]

    // assuming no field can ever have the character '#':
    $allfields = implode('#', array($row['a'], $row['b'], $row['c'], $row['d'], $row['e']));

    $validated = false;
    if (preg_match('^# ###$', $allfields) validated = true;
    if (preg_match('^. +#.+#.+#.+#.+$' , $allfields)) validated = true;

    // $validated is now true if the row is completely empty or completely full

    [color=blue]
    > Heres the kicker, the input fields in this form do not have a
    > consistant naming pattern all the fields have unique identifier.[/color]

    Ugh!
    [color=blue]
    > Also one column expect a date another an integer another text.[/color]

    The data you receive from the HTML form is *all* text!


    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Gleep

      #3
      Re: How should I go about validating a large form that has a "grid&quot ; of input fields and check boxes?

      On 29 May 2004 18:59:23 GMT, Pedro Graca <hexkid@hotpop. com> wrote:
      [color=blue]
      >Gleep wrote:[color=green]
      >> I've got an issue I'm stuck with. Imagine there is a large form that
      >> has 5 columns and 20 rows.
      >>
      >> In each row there is a check box - then 4 input fields. I already
      >> have the code that inserts all the data BUT the validation is a
      >> nightmare.
      >>
      >> What I need is. If an entire row is empty (not checked or filled out)
      >> that's OK. However if a user fills in one or two input fields within
      >> a row - that's NOT ok. The entire row needs to be filled out. It's
      >> an all or nothing situation.
      >>
      >> Lets say each column is called a, b, c, d, e
      >> I started writting various if statements but it instanly becomes out
      >> of control.
      >> example if a but not b and a and b and not c etc.... each row has
      >> many combinations.
      >>
      >> How can i structure php code that looks at each row and determines if
      >> the row is partically filled out, then valdiation fails . Validation
      >> passes if the row is complely empty or completely full.[/color]
      >
      >// assuming no field can ever have the character '#':
      >$allfields = implode('#', array($row['a'], $row['b'], $row['c'], $row['d'], $row['e']));
      >
      >$validated = false;
      >if (preg_match('^# ###$', $allfields) validated = true;
      >if (preg_match('^. +#.+#.+#.+#.+$' , $allfields)) validated = true;
      >
      >// $validated is now true if the row is completely empty or completely full
      >
      >[color=green]
      >> Heres the kicker, the input fields in this form do not have a
      >> consistant naming pattern all the fields have unique identifier.[/color]
      >
      >Ugh!
      >[color=green]
      >> Also one column expect a date another an integer another text.[/color]
      >
      >The data you receive from the HTML form is *all* text![/color]


      Ah yes now I see...
      turn each row into an array, implode by a unique character, then run ereg to examine the pattern.
      Brilliant! I was using a riduculous array of if statements. Thanks Pedro

      Comment

      • Pedro Graca

        #4
        Re: How should I go about validating a large form that has a &quot;grid&quot ; of input fields and check boxes?

        Gleep wrote:[color=blue]
        > On 29 May 2004 18:59:23 GMT, Pedro Graca <hexkid@hotpop. com> wrote:[color=green]
        >>Gleep wrote:[color=darkred]
        >>> How can i structure php code that looks at each row and determines if
        >>> the row is partically filled out, then valdiation fails . Validation
        >>> passes if the row is complely empty or completely full.[/color]
        >>
        >>// assuming no field can ever have the character '#':
        >>$allfields = implode('#', array($row['a'], $row['b'], $row['c'], $row['d'], $row['e']));
        >>
        >>$validated = false;
        >>if (preg_match('^# ###$', $allfields) validated = true;[/color][/color]
        // '/^####$/' $validated
        [color=blue][color=green]
        >>if (preg_match('^. +#.+#.+#.+#.+$' , $allfields)) validated = true;[/color][/color]
        // '/^.+#.+#.+#.+#.+ $/' $validated
        [color=blue][color=green]
        >>// $validated is now true if the row is completely empty or completely full[/color][/color]
        [color=blue]
        > Ah yes now I see...
        > turn each row into an array, implode by a unique character, then run ereg to examine the pattern.
        > Brilliant! I was using a riduculous array of if statements. Thanks Pedro[/color]

        Sorry for my previous errors -- just like this post it was written
        directly into my editor and not tested.


        I just figured another way that might be better :-)

        $validated = false;
        $count = 0;
        if (empty($row['a'])) ++$count;
        if (empty($row['b'])) ++$count;
        if (empty($row['c'])) ++$count;
        if (empty($row['d'])) ++$count;
        if (empty($row['e'])) ++$count;
        if (($count == 0) || ($count == 5)) $validated = true;

        --
        USENET would be a better place if everybody read: : mail address :
        http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
        http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
        http://www.expita.com/nomime.html : to 10K bytes :

        Comment

        • Leif Neland

          #5
          Re: How should I go about validating a large form that has a &quot;grid&quot ; of input fields and check boxes?

          Gleep wrote:[color=blue]
          > Lets say each column is called a, b, c, d, e
          > I started writting various if statements but it instanly becomes out
          > of control.
          > example if a but not b and a and b and not c etc.... each row has
          > many combinations.
          >
          > Validation passes if the row is complely empty or completely full.[/color]

          You have been given one solution.
          Another way is
          error = (a or b or c or d or e) and not (a and b and c and d and e)

          Leif


          Comment

          Working...