diff options
| author | M <m@lfurio.us> | 2015-12-05 18:54:01 -0500 | 
|---|---|---|
| committer | M <m@lfurio.us> | 2015-12-05 18:54:01 -0500 | 
| commit | 91659b121e63735a7620663c0f43f5c5adef77d4 (patch) | |
| tree | 090ec3f621f99b710c4a9c76b2c6bacb0fc1ab05 | |
| parent | 59962f7c260aaa0661b0c811e6b553d1a850032b (diff) | |
| download | scrott-91659b121e63735a7620663c0f43f5c5adef77d4.tar.gz scrott-91659b121e63735a7620663c0f43f5c5adef77d4.zip | |
+ Implemented populate function in Form class
+ Added helper function in Form class, logError
! Finished Form class for now
Diffstat (limited to '')
| -rw-r--r-- | app/class/form.class.php | 100 | 
1 files changed, 97 insertions, 3 deletions
| diff --git a/app/class/form.class.php b/app/class/form.class.php index ffee3d7..502e348 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -14,9 +14,15 @@ class Form          $this->numbFields = array();          $this->enumFields = array(); -        $this->errorlist   = array(); -        $this->warninglist = array(); -        $this->noticelist  = array(); +        $this->errorlist  = array(); +    } + +    /* +     * Log an error +     */ +    function logError($str) +    { +        $this->errorlist[] = $str;      }      /* @@ -67,6 +73,94 @@ class Form              'vals' => $values          );      } + +    /* +     * Populate the form with input data from web page +     */ +    function populate($input) +    { +        /* detect duplicate names */ +        $names = array(); +        foreach ($this->textFields as $fld) +            $names[] = $fld['name']; +        foreach ($this->numbFields as $fld) +            $names[] = $fld['name']; +        foreach ($this->enumFields as $fld) +            $names[] = $fld['name']; + +        if (count(array_unique($names)) != count($names)) +        { +            $this->logError("Internal error: Duplicate field names defined in form"); +            return false; +        } + +        /* init text fields */ +        foreach ($this->textFields as $fld) +        { +            if (isset($input[$fld['name']])) +                $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); + +            else if ($fld['req']) +                $this->logError($fld['name'] . " is required"); +        } + +        /* init numeric fields */ +        foreach ($this->numbFields as $fld) +        { +            if (isset($input[$fld['name']])) +            { +                if (!is_numeric($input[$fld['name']])) +                { +                    $this->logError($fld['name'] . " must be numeric"); +                    continue; +                } + +                if ($fld['int'] && (floor($input[$fld['name']]) != $input[$fld['name']])) +                { +                    $this->logError($fld['name'] . " must be an integer"); +                    continue; +                } + +                if (!is_null($fld['min']) && ($input[$fld['name']] < $fld['min'])) +                { +                    $this->logError($fld['name'] . " must be no less than " . $fld['min']); +                    continue; +                } + +                if (!is_null($fld['max']) && ($input[$fld['name']] > $fld['max'])) +                { +                    $this->logError($fld['name'] . " must be no more than " . $fld['max']); +                    continue; +                } + +                $this->$fld['name'] = $input[$fld['name']]; +            } + +            else if ($fld['req']) +                $this->logError($fld['name'] . " is required"); +        } + +        /* init enum fields */ +        foreach ($this->enumFields as $fld) +        { +            if (isset($input[$fld['name']])) +            { +                if (array_search($input[$fld['name']], $fld['vals']) === false) +                { +                    $this->logError($fld['name'] . " is not an appropriate value"); +                    continue; +                } + +                $this->$fld['name'] = $input[$fld['name']]; +            } + +            else if ($fld['req']) +                $this->logError($fld['name'] . " is required"); +        } + +        /* return */ +        return count($this->errorlist) == 0; +    }  }  ?> | 
