I've started to write a form data handler class in PHP. Can you please review it and point out any mistakes or logic errors, or perhaps if a method can be written in a better way?
define('VALIDATION_ERR', ' couldn`t be validated.');
define('REQUIRED_ERR', ' is required.');
define('PASSWORD_ERR', 'Password and Confirm Password do not match.');
class DataValidator {
protected $errorMsg;
protected $errorFlag;
public function __construct(){
$this->errorMsg = array();
$this->errorFlag = 0;
} // constructor
public function checkRequiredData( $dataArray , $requiredFields = NULL ){
if( count($dataArray) > 0 ){
if( $requiredFields != NULL ){
foreach( $dataArray as $key => $val ){
if( array_key_exists( $key , $requiredFields ) ){
$format = $requiredFields[$key];
if( !empty($val) && $format != 'none' ){
if( !($this->dataFormatCheck($val, $format)) ){
$msg = $key . VALIDATION_ERR ;
$this->setErrorMsg($msg);
$this->errorFlag = 1;
}
}
else{
$msg = $key . REQUIRED_ERR;
$this->setErrorMsg($msg);
$this->errorFlag = 1;
}
}
}
} // requiredFields array IF
else{
foreach( $dataArray as $key => $val ){
if( empty($val) ){
$msg = $key . REQUIRED_ERR;
$this->setErrorMsg($msg);
$this->errorFlag = 1;
}
}
}
} // requiredFields count IF
else{
$msg = "data array is empty";
$this->setErrorMsg($msg);
$this->errorFlag = 1;
}
if( $this->errorFlag == 0 ){
return true;
}
else{
return false;
}
} // checkRequiredData
public function getErrorMsg( $last = '' ){
if( $last == 'last' ){
return end($this->errorMsg);
}
else{
return $this->errorMsg;
}
} // getErrorMsg
public function setErrorMsg( $msg ){
array_push( $this->errorMsg, $msg );
} // setErrorMsg
public function dataFormatCheck( $data , $format ){
// use switch or if else?
switch( $format ){
case 'text' :
$return = true; // will be implemented later
break;
case 'email' :
$return = $this->checkEmail($data);
break;
case 'numeric' :
$return = $this->checkNumeric($data);
break;
case 'alphanumeric' : // will be implemented later
$return = true;
break;
case 'password' :
$return = $this->checkPassConfirm($data);
break;
case 'image' : // will be implemented later
$return = true;
break;
case 'none' : // will be implemented later
$return = true;
break;
}
return $return;
} // dataFormatCheck
public function checkAlpha( $data ){
// will be implemented later
} // check alphabets
public function checkEmail( $data ){
return filter_var($data, FILTER_VALIDATE_EMAIL);
} // check email
public function checkNumeric( $data ){
return is_numeric( $data );
} // check Numbers
public function checkAlphaNumeric( $data ){
// will be implemented later
} // check Alpha Numerics
public function checkPassConfirm( $pass , $cPass ){
if( $pass != $cPass )
return false;
else
return true;
}
}