05-17-2009, 09:21 PM
|
#10 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Here's my class. As you can see, it has parts of what WH provided earlier on...
I have some issues with this which I will be working on.
1) There's no way yet to correlate the errors with the data input. So right now all the errors get thrown into an array and then just printed out.
2) The string length check is minimum of 6. Not sure if this is good to be hardcoded.
3) Need to have a characters range method.
4) Sanitizing data input from SQL injects and buffer overflow. Don't know if this belongs in the validation class, or the database class.
5) Checking for existence of username/email already in the system.
PHP Code:
<?php
// VALIDATION CLASS class Validation {
private $_errorList = array(); const CHECK_EMPTY = '_checkEmpty'; const CHECK_EMAIL = '_checkEmail'; const CHECK_LENGTH = '_checkLength'; private $aValidationRules = array( 'name' => '[A-Za-z0-9\.|-|_/i]{2,20}', 'company' => '[A-Za-z0-9\.|-|_/i]{3,30}', 'address' => '[A-Za-z0-9\.|-|_/i]{3,50}', 'email' => '([^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4})', 'phone' => '(\s*\[?0\d{4}\]?\s*\d{6}\s*)|(\s*\[?0\d{3}\]?\s*\d{3}\s*\d{4}\s*)' );
public function __construct() { //RUN THIS METHOD TO CLEAR ERROR LIST AND START FRESH $this->_resetErrorList(); }
public function checkValid($szVariable) { $bValid = false; $aArgs = func_get_args(); array_shift($aArgs); foreach ($aArgs AS $szFunction) { if(!method_exists($this, $szFunction)) { throw new Exception(sprintf('Method does not exist: %s', $szFunction)); } $bValid = $this->$szFunction($szVariable); if(!$bValid) return false; } } private function _checkEmpty($szVariable) {
if(empty($szVariable)) { $this->_errorList[] = 'Data is Empty'; return false; }else{ return true; } } private function _checkEmail($szVariable) {
$pattern = $this->aValidationRules['email']; if(preg_match($pattern, $szVariable)) { return true; }else{ $this->_errorList[] = 'Not a valid email address'; return false; } } private function _checkLength($szVariable) {
if(strlen($szVariable) < 6) { $this->_errorList[] = 'Too short'; return false; }else{ return true; } } public function countErrors() { $count = count($this->_errorList); if ($count > 0) { return $count; } else { return false; } } public function getErrorList() { return $this->_errorList; } // THIS GETS CALLED ON NEW INSTANTIATED OBJECT TO START FRESH private function _resetErrorList() { $this->_errorList = array(); }
}//END Validation Class ?>
Last edited by allworknoplay : 05-17-2009 at 10:12 PM.
|
|
|
|