Far Cry Posted April 29, 2012 Share Posted April 29, 2012 So I have a validation class, and it puts all errors that occur within an array. However when I try and use foreach to get these values, only one gets returned. Even if there are multiple errors. validation.php: <?php class RegisterValidation { public $error = array(); public $valid; public function ValidateUsername($username) { /* X First sanatize username. X Then check length. Then chick if username exists in DB or not. */ trim($username); strip_tags($username); mysql_real_escape_string($username); $len = strlen($username); if ($len < 3 || $len > 20){ $this->valid = false; $this->error[] = "Your username must be 3 to 20 characters in length."; } else { $this->valid = true; } if(!$len < 3 || !$len > 20){ $checkusername = mysql_query("SELECT username FROM users WHERE username = '$username'"); if(mysql_mum_rows($checkusername) == 1){ // usename is found, throw out error $this->valid = false; $this->error[] = "That username is already in use."; } else { $this->valid = true; } } return $this->valid; } public function ValidatePassword($password,$repassword) { /* First sanatize. Then check if pass has at least one upper, lower, and one number. */ trim($password); strip_tags($password); mysql_real_escape_string($password); trim($repassword); strip_tags($repassword); mysql_real_escape_string($repassword); if(!preg_match('/[A-Z]/', $password)) { $this->valid = false; $this->error[] = "Your password must contain at least one upper-case letter."; } else { $this->valid = true; } if(!preg_match('/[a-z]/',$password)) { $this->valid = false; $this->error[] = "Your password must contain at least one lower-case letter."; } else { $this->valid = true; } if(!preg_match('/[0-9]/',$password)) { $this->valid = false; $this->error[] = "Your password must contain at least one number."; } else { $this->valid = true; } if($password !== $repassword){ $this->valid = false; $this->error[] = "Your passwords do not match."; } else { $this->valid = true; } return $this->valid; } public function ValidateEmail($email, $reemail){ /* Sanatize. User filter_var to validate. Make sure both emails match. Check if email already exists or not. */ trim($email); strip_tags($email); mysql_real_escape_string($email); trim($reemail); strip_tags($reemail); mysql_real_escape_string($reemail); if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $this->valid = false; $this->error[] = "The E-Mail that you provided is not valid."; } else { $this->valid = true; } if($email !== $reemail){ $this->valid = false; $this->error[] = "The E-Mail's that you provided do not match."; } else { $this->valid = true; } if(filter_var($email, FILTER_VALIDATE_EMAIL) && $email == $reemail){ $checkmail = mysql_query("SELECT users.email, temp_users.email FROM users, temp_users WHERE email = '$email'"); if(mysql_mum_rows($checkmail) == 1){ // email is found, throw out error $this->valid = false; $this->error[] = "That E-Mail is already in use."; } else { $this->valid = true; } } return $this->valid; } ?> register.php (snippet): if($register->ValidateUsername($username) && $register->ValidatePassword($password, $repassword) && $register->ValidateEmail($email, $reemail)) { //Add user } else { foreach($register->error as $error){ echo "<span class=\"error\">".$error."</span>"; } } Thanks! Quote Link to comment Share on other sites More sharing options...
creata.physics Posted April 30, 2012 Share Posted April 30, 2012 I couldn't see why it wasn't giving you all the errors. I ended up cleaning up your code a bit, it's used differentlyso maybe you'll like it, maybe you wont. <?php $register = new RegisterValidation; $register->ValidateUsername('m'); $register->ValidatePassword('matt','maatt'); $register->ValidateEmail('matt','matta'); if( $register->GetStatus() == true ) { echo 'everything was valid'; } else { foreach( $register->errors as $error ) { // output each message echo $error . '<br/>'; } } class RegisterValidation { public $errors = array(); public $valid = true; public function ValidateUsername($username) { /* X First sanatize username. X Then check length. Then chick if username exists in DB or not. */ trim($username); strip_tags($username); #mysql_real_escape_string($username); $len = strlen($username); if ($len < 3 || $len > 20) { $this->errors[] = "Your username must be 3 to 20 characters in length."; } if(!$len < 3 || !$len > 20) { /*$checkusername = mysql_query("SELECT username FROM users WHERE username = '$username'"); if( mysql_mum_rows( $checkusername ) == 1 ) { // usename is found, throw out error $this->errors[] = "That username is already in use."; }*/ } if( count( $this->errors ) > 0 ) { $this->valid = false; } } public function ValidatePassword($password,$repassword) { /* First sanatize. Then check if pass has at least one upper, lower, and one number. */ trim($password); strip_tags($password); #mysql_real_escape_string($password); trim($repassword); strip_tags($repassword); #mysql_real_escape_string($repassword); if(!preg_match('/[A-Z]/', $password)) { $this->errors[] = "Your password must contain at least one upper-case letter."; } if(!preg_match('/[a-z]/',$password)) { $this->errors[] = "Your password must contain at least one lower-case letter."; } if(!preg_match('/[0-9]/',$password)) { $this->errors[] = "Your password must contain at least one number."; } if($password !== $repassword) { $this->errors[] = "Your passwords do not match."; } if( count( $this->errors ) > 0 ) { $this->valid = false; } } public function ValidateEmail($email, $reemail) { /* Sanatize. User filter_var to validate. Make sure both emails match. Check if email already exists or not. */ trim($email); strip_tags($email); #mysql_real_escape_string($email); trim($reemail); strip_tags($reemail); #mysql_real_escape_string($reemail); if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $this->errors[] = "The E-Mail that you provided is not valid."; } if($email !== $reemail) { $this->errors[] = "The E-Mail's that you provided do not match."; } if(filter_var($email, FILTER_VALIDATE_EMAIL) && $email == $reemail) { /*$checkmail = mysql_query("SELECT users.email, temp_users.email FROM users, temp_users WHERE email = '$email'"); if(mysql_mum_rows($checkmail) == 1) { // email is found, throw out error $this->errors[] = "That E-Mail is already in use."; }*/ } if( count( $this->errors ) > 0 ) { $this->valid = false; } } public function GetStatus() { return $this->valid; } } And it outputs: Your username must be 3 to 20 characters in length. Your password must contain at least one upper-case letter. Your password must contain at least one number. Your passwords do not match. The E-Mail that you provided is not valid. The E-Mail's that you provided do not match. Hope I helped. Oh, I forgot to mention I commented out mysql releated stuff so I wouldn't trip any fatal errors. You'll need to take out the comments to do your checks for duplicate data. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 30, 2012 Share Posted April 30, 2012 trim($password); strip_tags($password); mysql_real_escape_string($password); ^^^ Just calling those functions doesn't do anything, because each of those functions returns the resulting string to the calling code. You must assign or use the return value. Quote Link to comment Share on other sites More sharing options...
Zane Posted April 30, 2012 Share Posted April 30, 2012 or at least put them in as parameters by reference trim(&$password) Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 30, 2012 Share Posted April 30, 2012 Only some of the array functions modify the source values that they expect to be passed by reference. None of the functions in this thread are coded to do so and produce a 'Call-time pass-by-reference has been deprecated' error to boot. <?php $a = ' s '; // space s space trim(&$a); var_dump($a); // string(3) " s " Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.