For the most part I found two errors
The first was a scope problem. $HTTP_*_VARS hashes are not accessible
inside functions unless you source them in. i.e. global
The other was an extra line in an if statement that did not contain braces.
I did a little formating while I checked the code. Try this to see if it
works as you expected it to.
<?php
class FormV {
var $errorList;
function FormV() {
$this->reset_errorList();
}
function getValue($field) {
global $HTTP_POST_VARS;
return $HTTP_POST_VARS[$field];
}
function isEmpty($field, $msg) {
$value = $this->getValue($field);
if(!trim($value)) {
$this->errorList[] = array('field'=>$field,
'value'=>$value, 'msg'=>$msg);
return true;
} else
return false;
}
function isString($field, $msg) {
$value = $this->getValue($field);
if(is_string($value))
return true;
else {
$this->errorList[] = array('field'=>$field,
'value'=>$value,'msg'=>$msg);
return false;
}
}
function isEmail($field, $msg) {
$value = $this->getValue($field);
$pattern = '/^([a-zA-Z0-9])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/';
if(preg_match($pattern, $value))
return true;
else {
$this->errorList[] = array('field'=>$field,
'value'=>$value, 'msg'=>$msg);
return false;
}
}
function isError() {
if(sizeof($this->errorList) > 0)
return true;
else
return false;
}
function get_errorList() {
return $this->errorList;
}
function reset_errorList() {
$this->errorList = array();
}
function stringConvert($field) {
$value = $this->getValue($field);
$value = html_special_chars($value);
$value = stripslashes($value);
$value = strtolower($value);
return $value;
}
}
?>
----- Original Message -----
From: "Nicholas Wieland" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, January 21, 2003 8:12 AM
Subject: [PHP] Form Validating Class (OOP misunderstandings...)
> Hello everybody,
> I'm working on a class but having a lot of problems... probably my
> understanding of PhP-OOP is not so good ...
> Here's the class: I found it on the web and I've tried to personalize
> it to fit my needs...
>
> <?php
>
> class FormV
> {
> var $errorList;
>
> function FormV()
> {
> $this->reset_errorList();
> }
>
> function getValue( $field )
> {
> $value = $HTTP_POST_VARS[ $field ];
> return $value;
> }
>
> function isEmpty( $field, $msg )
> {
> $value = $this->getValue( $field );
>
> if( trim( $value ) == "" )
> {
> $this->errorList[] = array( "field" => $field,
> "value" => $value, "msg" => $msg );
> return true;
> }
> else
> return false;
>
> }
>
> function isString( $field, $msg )
> {
> $value = $this->getValue( $field );
>
> if( is_string( $value ) )
> return true;
>
> else
> {
> $this->errorList[] = array( "field" => $field,
> "value" => $value, "msg" => $msg );
> return false;
> }
> }
>
> function isEmail( $field, $msg )
> {
> $value = $this->getValue( $field );
> $pattern =
> "/^([a-zA-Z0-9])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
>
> if( preg_match( $pattern, $value ) )
> return true;
>
> else
> {
> $this->errorList[] = array( "field" => $field,
> "value" => $value, "msg" => $msg );
> return false;
> }
> }
>
> function isError()
> {
> if( sizeof( $this->errorList ) > 0 )
> return true;
>
> else
> return false;
> }
>
> function get_errorList()
> {
> return $this->errorList;
> }
>
> function reset_errorList()
> {
> $this->errorList = array();
> }
>
> function stringConvert( $field )
> {
> $value = $this->getValue( $field );
>
> $value = html_special_chars( $value );
>
> $value = stripslashes( $value );
>
> $value = strtolower( $value );
>
> return $value;
> }
>
> };
>
> ?>
>
> Ok.
> I think that the getter is *totally* wrong but I'm not able to debug
> it, because it's private (I think...). Also the stringConvert() method
> sucks, maybe I'll split it in some others accessor methods, for a
> better design.
> Now I'm totally lost, I don't have a clue how to continue, I've tried
> for hours and hours to make it work, but didn't succeed :(
> Any suggestion on improving and other enhancment for a good form
> validating class is really really appreciated.
>
> TIA,
> Nicholas
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php