RE: [PHPLIB] altering validate() From: Alan Lee (alee <email protected>)
Date: 11/11/99

If you want to tell your users which element of an array valued form
input was invalid, here's how.

Edit of_text.inc

copy the entire function self_validate(), then:

  function self_validate($val) {
    if (!is_array($val)) $val = array($val);
    else return $this->array_self_validate($val); /* add this line */

// } // end TEXT /* comment out this
line */

then paste in the function you copied, changing it's name:

  function array_self_validate($val) { /* change the name */
    reset($val);
    $i = 0; /* add this line for a
counter */
    while (list($k,$v) = each($val)) {
      $i++; /* add this line to
count */
      if ($this->length_e && (strlen($v) < $this->minlength))
        return array($i, $this->length_e); /* return an array for
yourself */
      if ($this->valid_e && (($this->icase &&
            !eregi($this->valid_regex,$v)) ||
           (!$this->icase &&
            !ereg($this->valid_regex,$v))))
        return array($i, $this->valid_e); /* return another
array for yourself */
    }
    return false;
  }

} // end TEXT

Now in your web document, after you do $err = $f->validate();
you can do:
  
    if($err) echo "Section $err[0]: $err[1]";

which will come out: "Section 4: That input is no good. Do again!"

A.
-
PHP3 Base Library Mailing List. Send messages to <phplib <email protected>>.
To unsubscribe, send "unsubscribe" to <phplib-request <email protected>> in
the body, not the subject, of your message.