Page 1 of 1

Problems with some errors and with class

Posted: Tue Apr 24, 2007 1:03 pm
by staar2
I get this errors Notice: Undefined offset: 1 in E:\wamp\www\classes\class.validate.php on line 57 and method addField is bugy and i dont know what to do now?

Code: Select all

<?php
include_once('functions.php');
error_reporting(E_ALL); 
	class Val {
		
		private $_vars = array(); #$_POST muutujad
		private $_errors = array(); #errori muutuja kujul [field] => error
		
		public function __construct() {
			$this->getPost();
		}
		
		/*
		Võtame $_POST-st saadud väärtused isegi nupu omad
		*/
		private function getPost() {
			foreach($_POST as $field => $value) {
				if (isset($value)) {
					$this->_vars[$field] = $value;
				}
			}
		}
		
		/*
		Tagastame $_POST väärtused [field] => value
		*/
		public function returnField() {
			return $this->_vars;
		}
		
		/*
		Lisab välja ning ka sinna kuuluvad võimalused, mida kontrollida.
		Algul vaatab kas üldse selline field on olemas. Hea on kui mõlemad muutujad saaks strtolower-iga üle tehtud
		*/
		private function checkFieldExist($fieldName) {			
			foreach ($this->_vars as $key => $value) {
				if ($key == $fieldName) {
					return true;
				}
			}
			return false;
		}
		
		private function checkMethod($fieldName) {
				
		}
		
/*
Here begin problems

*/
		public function addField($name) {
			if ($this->checkFieldExist($name)) {
				$func_args = func_get_args();
				
				if (is_array($func_args)) {
					for ($i = 1; $i < count($func_args); $i++) {
						$fields[] = explode('::', $func_args[$i]);
						
						$func = new Func();
						if (method_exists($func, $fields[$i][0])) {
							if (!call_user_method($fields[$i][0], $func, $this->_vars[$name])) {
								$this->addError($name, $fields[$i][1]);
							}
						}
					}				
				}
				
			} else {
				throw new Exception('There is no such input field!');
			}
		}
		
		/*
		Lisame errori [field] => value
		*/
		private function addError($field, $value) {
			$this->_errors[$field] = $value;
		}
		/*
		Tagasatb massiivina errorid koos field ja väärtusega
		*/
		public function getErrors() {
			return $this->_errors;
		}

	}
	

	
if (isset($_POST['saada']) && $_POST['saada']) {

	$v = new Val();
	
	/*IDEE ET panna nimi::Nime lahter on tühjaks jäänud
	isNum::Peaks olema number
	isEmpty::Peaks midagi ikka sisse ka panema
	*/
	
	echo $v->addField('nimi', 'isNum::On number', 'isEmpty::kas on tühi');


	
}
?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
	<p>Nimi: <input type="text" name="nimi" /></p>
	<p>E-mail: <input type="text" name="email" /></p>
	<p>Eesnimi: <input type="text" name="firstname" /></p>
	<p>Perekonnanimi: <input type="text" name="lastname" /></p>
	<p><input type="submit" name="saada" value="sisesta" /></p>
</form>
Functions.php

Code: Select all

<?php
	class Func {
		
		public function isString($str) {
			return is_string($str) ? true : false;
		}
		
		public function isNum($str) {
			return is_numeric($str) ? true : false;
		}
		
		public function isEmpty($str) {
			$str = strlen($str);
			return $str > 0 ? true : false;
		}
	}
?>
:roll:

Posted: Tue Apr 24, 2007 1:27 pm
by jmut

Code: Select all

public function addField($name) { 
...
$func_args = func_get_args(); 
..
$fields[] = explode('::', $func_args[$i]); 
...
}
I assume this line is the problem is you start itterating on index 1.
If you do not pass any more arguments than $name you will have only index 0.