georgee.b Posted July 27, 2009 Share Posted July 27, 2009 I am trying to write a function for form validation, however i am having a difficult time accessing the array of arrays. I push the object into array and know the path of how to retrieve it, but i do not know how to do it programatically. PHP validator class <?php class Validator { var $validator_array = array(); function __construct() { } public function validator($field_name, $number_chars, $character_check, $error) { $new_validator = new self; //This is the long-form approach $new_validator->field_name = $field_name; $new_validator->number_chars = $number_chars; $new_validator->character_check = $character_check; $new_validator->errors = $error; array_push($this->validator_array, $new_validator); //This is the short-form loop } public function checkArray() { $object = new self; foreach ($this->validator_array as $array_position=>$field) { foreach ($field as $field=>$fieldName) { $object->$field = $fieldName; } } return $object; } } $validator = new Validator(); ?> HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Updating existing Beverage</title> <link rel="stylesheet" type="text/css" href="../css/main.css" /> </head> <?php require_once('../../application/includes/init.php'); include("header.php"); if ($_POST) { if (!empty($_POST)) { $beverage_name = $validator->validator("beverage_name", "30", "l", "The name is too long"); $beverage_price = $validator->validator("beverage_tag", "2", "n", "The description is quite lenghty"); $result = $validator->checkArray(); print_r($result); } HOW do i iterate through these objects //echo $validator->validator_array[0]->field_name; //echo $validator->validator_array[0]->number_chars; } ?> <body> <div class="editing_form"> <h4>Update all the important details about a beverage</h4> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr> <td class="bev_edit">Beverage Name:</td> <td><input type="text" class="textfield" name="beverage_name" value=""/></td> </tr> <tr> <td class="bev_edit">Beverage Tag: $</td> <td><input type="text" class="textfield" name="beverage_tag" value=""/> </td> </tr> <tr> <td class="bev_edit"></td> <td><input type="submit" class="textfield" name="Submit" value="Update" /></td> </tr> </table> </form> </div> </body> </html> I need to be able to call the objects with their attributes and then init some kind of function that will ouput an error message beside each field, when an error occures Any suggestions Thanks Quote Link to comment https://forums.phpfreaks.com/topic/167587-iterating-through-object-array-issue/ Share on other sites More sharing options...
ignace Posted July 27, 2009 Share Posted July 27, 2009 Use Zend_Form and Zend_Validator to accomplish this. You'll otherwise have to write to much. Quote Link to comment https://forums.phpfreaks.com/topic/167587-iterating-through-object-array-issue/#findComment-883822 Share on other sites More sharing options...
KevinM1 Posted July 27, 2009 Share Posted July 27, 2009 If you still want to roll your own, I suggest separating the responsibilities of your current class. Right now, your class is attempting to do too much. From what I can see, it's attempting to hold all validators (which are other objects of the same type) and perform validation. Also, you're dynamically adding new properties to it with the $object->field = $fieldname stuff, which is a very bad idea. So, here's what you should do to get started: 1. Create a new class whose sole job is to hold an array of validator objects and iterate over them. Something like: class ValidatorArray { private $validators = array(); public function add(Validator $validator) { $this->validators[] = $validator; } public function validate() { foreach($this->validators as $validator) { $validator->validate() } } public function getResults() { foreach($this->validators as $validator) { if(!$validator->didPass()) { echo $validator->getError() . "<br />"; } } } } It may not be exactly what you need, but you should get the idea. 2. Create a Validator class that has the private properties you need to get the job done. No creating new public properties on the fly. class Validator { private $fieldName; private $numChars; private $charCheck; private $passed = false; private $errMsg; public function __construct($fieldName, $numChars, $charCheck, $errMsg) { $this->fieldName = $fieldName; $this->numChars = $numChars; $this->charCheck = $charCheck; $this->$errMsg = $errMsg; } public function validate() { //perform validation logic if(/* data validated */) { $this->passed = true; } else { $this->passed = false; } } public function didPass() { return $this->passed; } public function getError() { return $this->errMsg; } } Again, it may not be exactly what you need, but you should get the idea. Quote Link to comment https://forums.phpfreaks.com/topic/167587-iterating-through-object-array-issue/#findComment-883846 Share on other sites More sharing options...
ignace Posted July 27, 2009 Share Posted July 27, 2009 2. Create a Validator class that has the private properties you need to get the job done. No creating new public properties on the fly. class Validator { private $fieldName; private $numChars; private $charCheck; private $passed = false; private $errMsg; public function __construct($fieldName, $numChars, $charCheck, $errMsg) { $this->fieldName = $fieldName; $this->numChars = $numChars; $this->charCheck = $charCheck; $this->$errMsg = $errMsg; } public function validate() { //perform validation logic if(/* data validated */) { $this->passed = true; } else { $this->passed = false; } } public function didPass() { return $this->passed; } public function getError() { return $this->errMsg; } } You are not always only validating if a string has a certain length: abstract class Validator { abstract public function performValidation(ValidatorArray $va); } class StringLengthValidator extends Validator { protected $_strLen = 0; protected $_minLen = 0; protected $_maxLen = 0; public function __construct($string, $minLenght, $maxLength) { $this->_strLen = strlen($string); $this->_minLen = $minLength; $this->_maxLen = $maxLength; } public function performValidation(ValidatorArray $va) { $valid = true; if ($this->_strLen < $minLength) { $va->addErrorMsg('To short'); $valid = false; } else if ($this->_strLen > $maxLength) { $va->addErrorMsg('To long'); $valid = false; } return $valid; } } class ValidatorArray { protected $_errorMsgs = array(); protected $_passed = array(); public function getErrorMsgs() { return $this->_errorMsgs; } public function addErrorMsg($errorMsg) { $this->_errorMsgs[] = $errorMsg; } public function validate() { foreach ($this->_validators as $validator) { $className = get_class($validator); $this->_passed[$className] = $validator->performValidation($this); } } public function didPass($validator) { return isset($this->_passed[$validator]) && $this->_passed[$validator]; } } Quote Link to comment https://forums.phpfreaks.com/topic/167587-iterating-through-object-array-issue/#findComment-883857 Share on other sites More sharing options...
KevinM1 Posted July 27, 2009 Share Posted July 27, 2009 Oh, I know. I just wanted to give the OP a nudge in the right direction in terms of general class structure. Quote Link to comment https://forums.phpfreaks.com/topic/167587-iterating-through-object-array-issue/#findComment-883864 Share on other sites More sharing options...
georgee.b Posted July 27, 2009 Author Share Posted July 27, 2009 I just have a few questions, perhaps you can direct me in the right direction. I am confused as far as the OOP is concerned, as there are so many variables and functions, that i keep forgetting which class does what. Can you direct me to a resource or a method which helped you master OOP in PHP. Anyway The code i placed, however it is failing because __constructor is not getting any values PHP Classes in according order: <?php class Validator { private $field_name; private $number_chars; private $character_check; private $allChars; private $errMsg; private $passed = false; public function __construct($field_name, $number_chars, $allowed_chars, $character_check, $error) { $this->field_name = $field_name; $this->number_chars = $number_chars; $this->character_check = $character_check; $this->errMsg = $error; $this->allChars = $allowed_chars; } public function validate() { if ($this->number_chars <= $this->allChars) { $this->passed = true; } else { $this->passed = false; } } public static function sanitizeData($value) { $value = trim($value); return $value; } public function didPass() { return $this->passed; } public function getError() { return $this->errMsg; } } $validator = new Validator(); ?> Next class as you recommended <?php class ValidateArray { private $validators = array(); public function add(Validator $validator) { $this->validators[] = $validator; } public function validate() { foreach ($this->validators as $validator) { $validator->validate(); } } public function getResults() { foreach ($this->validators as $validator) { if (!$validator->didPass()) { echo $validator->getError() . "<br />"; } } } } $validateArr = new ValidateArray(); ?> HTML code if (!empty($_POST)) { $beverage_name = $validateArr->add("beverage_name", "30", "30", "l", "The name is too long"); $validateArr->validate(); $validateArr->getResults(); And finally the Error Warning: Missing argument 1 for Validator::__construct(), called in C:\wamp_web\me\application\includes\validator.php on line 38 and defined in C:\wamp_web\me\application\includes\validator.php on line 11 Warning: Missing argument 2 for Validator::__construct(), called in C:\wamp_web\me\application\includes\validator.php on line 38 and defined in C:\wamp_web\me\application\includes\validator.php on line 11 Warning: Missing argument 3 for Validator::__construct(), called in C:\wamp_web\me\application\includes\validator.php on line 38 and defined in C:\wamp_web\me\application\includes\validator.php on line 11 Warning: Missing argument 4 for Validator::__construct(), called in C:\wamp_web\me\application\includes\validator.php on line 38 and defined in C:\wamp_web\me\application\includes\validator.php on line 11 Warning: Missing argument 5 for Validator::__construct(), called in C:\wamp_web\me\application\includes\validator.php on line 38 and defined in C:\wamp_web\me\application\includes\validator.php on line 11 Notice: Undefined variable: field_name in C:\wamp_web\me\application\includes\validator.php on line 12 Notice: Undefined variable: number_chars in C:\wamp_web\me\application\includes\validator.php on line 13 Notice: Undefined variable: character_check in C:\wamp_web\me\application\includes\validator.php on line 14 Notice: Undefined variable: error in C:\wamp_web\me\application\includes\validator.php on line 15 Notice: Undefined variable: allowed_chars in C:\wamp_web\me\application\includes\validator.php on line 16 Quote Link to comment https://forums.phpfreaks.com/topic/167587-iterating-through-object-array-issue/#findComment-884107 Share on other sites More sharing options...
ignace Posted July 28, 2009 Share Posted July 28, 2009 That's because you need to pass it some values: $validator = new Validator(); Should be: $validator = new Validator($field_name, $number_chars, $allowed_chars, $character_check, $error); Quote Link to comment https://forums.phpfreaks.com/topic/167587-iterating-through-object-array-issue/#findComment-884769 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.