Drongo_III Posted June 14, 2011 Share Posted June 14, 2011 Hi Guys I'm a noobie to this...still. I'm starting to get into oop and I've tried to right a simple validation class. The code I've created (assuming it's right) takes the POST array, runs a validation array against it and then iterates through the validation array to see if there are any empty values. My thought was is there's an empty value it should return false, stop the itterating through the array and thereby i'll know something isn't validated and i can execute some code (at the moment it just echos either valid or invalid) I think i have gone wrong on the foreach part though because if the first array element is ok then it just displays that everything is valid - even if the other array elements are empty. I think maybe the "break" statement isn't doing what i think it should and stopping the loop. But i may be totally wrong. Can anyone tell me where i'm going wrong please and suggest a better method along the lines of what i have created. Many thanks, Drongo - noobie phper :/ class registration { // Validatation options array public $validation_options = array( 'login' =>array('filter'=>FILTER_VALIDATE_EMAIL), 'password' =>array('filter'=>FILTER_VALIDATE_STRING), 'age' =>array('filter'=>FILTER_VALIDATE_INT) ); // Filter user input and direct accordingly function filters() { $this->validated = filter_input_array(INPUT_POST, $this->validation_options ); foreach($this->validated as $key => $value) { if (empty($value)) { return false; break; } else { return true; } } } function activate() { $this->filters(); if ($this->filters() == false) { echo "Not valid sorry"; } elseif ($this->filters() == true) { echo "all valid my son"; } } } $reg = new registration(); //$db->filters(); $reg->activate(); ?> Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted June 14, 2011 Share Posted June 14, 2011 The problem is you are returning true in your foreach loop. What happens is you test if something is empty. If its not you return true. What you want to do is test if something is empty and return false. then at the end of your foreach loop you can safely return true (because if you needed to return false, you would have already) something like function filters() { $this->validated = filter_input_array(INPUT_POST, $this->validation_options); foreach($this->validated as $key => $value) { if (empty($value)) { return false; //break; this is unecessary and doesnt even happen. if we return then the loop is automatically exited } } return true;//we checked everything. we didnt return false yet so return true } EDIT: formatted the code i posted nicely Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted June 14, 2011 Author Share Posted June 14, 2011 Hi Mike Well that did the trick! I knew my thinking had gone astray somewhere. I didn't realise returning in a loop ended the loop. My unenlightened logic thought that if it found an empty instance then it would return false and the break would stop the loop. Lesson learned. Your code worked a treat Thank you very much! Drongo The problem is you are returning true in your foreach loop. What happens is you test if something is empty. If its not you return true. What you want to do is test if something is empty and return false. then at the end of your foreach loop you can safely return true (because if you needed to return false, you would have already) something like function filters() { $this->validated = filter_input_array(INPUT_POST, $this->validation_options); foreach($this->validated as $key => $value) { if (empty($value)) { return false; //break; this is unecessary and doesnt even happen. if we return then the loop is automatically exited } } return true;//we checked everything. we didnt return false yet so return true } EDIT: formatted the code i posted nicely 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.