blueman378 Posted December 21, 2008 Share Posted December 21, 2008 Hi guys, well im making a form validator for me, anyway heres the code ill explain it below <?php //------------------- //IF SESSION HASNT BEEN STARTED START IT //------------------- if(session_id() == "") { session_start(); } //------------------- //INCLUDE ALL VALIDATION TYPES //------------------- if ($handle = opendir('validations')) { while (false !== ($file = readdir($handle))) { if($file != "." && $file != "..") { include("validations/$file"); } } closedir($handle); } //------------------- //THE MAIN CLASS //------------------- class form { var $values = array(); //the values that the form holds var $errors = array(); //any errors reported from the form var $valtype = array(); //the different validation types that are needed var $num_errors; /* Class constructor */ function form(){ if(isset($_SESSION['error_array'])){ //if the error_array session is set $this->values = $_SESSION['value_array']; //fill the class variables with them $this->errors = $_SESSION['error_array']; //this one too $this->num_errors = count($this->errors); //then count the number of errors unset($_SESSION['value_array']); //we dont need this anymore unset($_SESSION['error_array']); //or this } else{ //if its not set there are no errors. $this->num_errors = 0; } } function setValue($field, $value){ //redundant atm will use it later $this->values[$field] = $value; } function setError($field, $errmsg){ //set error messages called from the validation $this->errors[$field] = $errmsg; //classes $this->num_errors = count($this->errors); //recount the errors $_SESSION['value_array'] = $this->values; //update the sessions $_SESSION['error_array'] = $this->errors; //this one too. redundant atm } function value($field){ //another redundant function if(array_key_exists($field,$this->values)){ return htmlspecialchars(stripslashes($this->values[$field])); }else{ return ""; } } function error($field){ //used to display the error if(array_key_exists($field,$this->errors)){ return "<font size=\"2\" color=\"#ff0000\">".$this->errors[$field]."</font>"; }else{ return ""; } } function addvaltype($field, $valtype) //used to add a validation type { $this->valtype[$field] = $valtype; } function validate() //this function calls the validate function of the { //different classes foreach ($this->valtype as $field => $val) { global $$val; //theres got to be a better way than defining the //other classes as global? $$val->validate($_POST[$field], $field); //now call the sub validate functions } header("location: $_SERVER[php_SELF]"); //once thats done refresh the page } } $form = new form; ?> <?php // VALIDATION TYPE: alpha // VALIDATION DESC: only allow letters class alpha extends form { function alpha() { } function validate($input, $field) { if(!isset($input) || $input == "" || $input == NULL) { $form->setError($field, "$field cannot be left empty!"); return 1; } elseif(!ctype_alpha($input)) { $form->setError($field, "$field can only contain letters [a-z]!"); return 1; } else { return 0; } } } $alpha = new alpha; ?> <?php // VALIDATION TYPE: numeric // VALIDATION DESC: only allow numbers class numeric extends form { function numeric() { } function validate($input, $field) { if(!isset($input) || $input == "" || $input == NULL) { $this->setError($field, "$field cannot be left empty!"); return 1; } elseif(!ctype_digit($input)) { $this->setError($field, "$field can only contain numbers [0-9]!"); return 1; } else { return 0; } } } $numeric = new numeric; ?> <?php // VALIDATION TYPE: alphanum // VALIDATION DESC: only allow letters and numbers class alphanum extends form { function alphanum() { } function validate($input, $field) { if(!isset($input) || $input == "" || $input == NULL) { $this->setError($field, "$field cannot be left empty!"); return 1; } elseif(!ctype_alnum($input)) { $this->setError($field, "$field can only contain letters [a-z] and numbers [0-9]!"); return 1; } else { return 0; } } } $alphanum = new alphanum; ?> so there general idea is that as you can see in test.php you have your form which looks normal, but above that you have: $form->addvaltype("alphanumtest2", "alphanum"); this is basically adding the form name and its validation type to an array stored in $form then when you click submit the form class initializes the validate function of each validation type that is in the array. if ther are errors they are reported back, this all works fine except only one validation type initializes at a time, so looking at these three lines: $form->addvaltype("alphanumtest", "alphanum"); $form->addvaltype("lol", "numeric"); $form->addvaltype("alphanumtest2", "alphanum"); only the ones marked with alphanum would be validated if it was $form->addvaltype("alphanumtest", "numeric"); $form->addvaltype("lol", "numeric"); $form->addvaltype("alphanumtest2", "alphanum"); then only the ones with numeric would be validated and if it was $form->addvaltype("alphanumtest", "alpha"); $form->addvaltype("lol", "numeric"); $form->addvaltype("alphanumtest2", "alphanum"); then only the one with alhpa would be validated and i cant for the life of me work out why. any ideas? sorry the tabbing is messed up Link to comment https://forums.phpfreaks.com/topic/137895-seems-like-only-one-class-is-initializing/ Share on other sites More sharing options...
blueman378 Posted December 21, 2008 Author Share Posted December 21, 2008 *bump* Link to comment https://forums.phpfreaks.com/topic/137895-seems-like-only-one-class-is-initializing/#findComment-720965 Share on other sites More sharing options...
blueman378 Posted December 22, 2008 Author Share Posted December 22, 2008 double bump. Please let me know if im bumping too soon btw. i bump when it goes off the second page. Link to comment https://forums.phpfreaks.com/topic/137895-seems-like-only-one-class-is-initializing/#findComment-721230 Share on other sites More sharing options...
trq Posted December 22, 2008 Share Posted December 22, 2008 I've looked at this code several times today but haven't really had the time to go into any explanation. Simply put, it needs to be completely redesigned. Firstly, your alpha class references a $form object, this object does not exist within the alpha class. Secondly, the rest of your objects are storing errors within $this->errors. I hope you not expecting these to show up within your form classes $this->errors because they won't. Thirdly, this part.... foreach ($this->valtype as $field => $val) { global $$val; //theres got to be a better way than defining the //other classes as global? $$val->validate($_POST[$field], $field); //now call the sub validate functions } header("location: $_SERVER[php_SELF]"); what can I say. That's a pretty messy area and the call to header() will completely wipe out anything stored within your form object. I really don't have the time to suggest remarkable improvements at the moment, but IMO the entire process needs to be rethought and and redesigned. Link to comment https://forums.phpfreaks.com/topic/137895-seems-like-only-one-class-is-initializing/#findComment-721236 Share on other sites More sharing options...
blueman378 Posted December 22, 2008 Author Share Posted December 22, 2008 hmm cheers, i think ill just scrap this and use a premade one eh. dont really have classes down. anyway can you reccomend any good form validation classes other than clonefish? Link to comment https://forums.phpfreaks.com/topic/137895-seems-like-only-one-class-is-initializing/#findComment-721392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.