As what usually happens when you first right some code, and then put it into real world use, you make some changes to make it more efficient and easier to use. The same thing happened with my CheckFields function posted earlier.
So here’s the code:
‘year’, ‘email’ => ‘email’, ‘date’ => ‘date’, ‘length=2, int, date’ => ‘month’, ‘passwordsmatch’ => ‘password, confirmpass’);
$input = array | example=($_POST || $_GET)*/
class CheckFields { public $lastError;
public function __construct($reqs, $input) { foreach ($reqs as $type => $id) { $fields = explode(’, ‘, $id); foreach ($fields as $field) { if (!isset($input[$field]) || $input[$field] == ”) { $this->_ret(false, 00, $id, ‘”‘ . ucfirst($id) . ‘” Must be completed in order to continue.’); break(2); //return array(false, $id . ‘ resulted in err00′); } }
$types = explode(’, ‘, $type); foreach ($types as $t) { switch (preg_replace(’|(^.+)(\=.*)|’, ‘$1′, $t)) { case ‘passwordsmatch’: $fields = explode(’, ‘, $id); if ($input[$fields[0]] != $input[$fields[1]]) { $this->_ret(false, 06, $id, ‘”‘ . ucfirst($fields[0]) . ‘” must be the same as “‘ . ucfirst($fields[1]) . ‘”‘); break(3); //return array(false, $id . ‘ resulted in err06′); } break; case ‘date’: if (strtotime($input[$id]) === false) { $this->_ret(false, 05, $id, ‘”‘ . ucfirst($id) . ‘” Is not a valid date.’); break(3); //return array(false, $id . ‘ resulted in err05′); } break; case ‘email’: if (!eregi(”^(.+)(@)(.+)(\.)(.+)$”, $input[$id])) { $this->_ret(false, 04, $id, ‘”‘ . ucfirst($id) . ‘” Is not a valid email address.’); break(3); //return array(false, $id . ‘ resulted in err04′); } break; case ‘length’: // Make sure the field is the correct length preg_match(’|(^.+)(\=)(.*$)|’, $t, $m); if (strlen($input[$id]) != $m[3]) { $this->_ret(false, 01, $id, ‘”‘ . ucfirst($id) . ‘” Is not the correct length.’); break(3); //return array(false, $id . ‘ resulted in err01′); } break; case ‘int’: if (!is_numeric($input[$id])) { $this->_ret(false, 02, $id, ‘”‘ . ucfirst($id) . ‘” Must be a number and it is not.’); break(3); //return array(false, $id . ‘ resulted in err02′); } break; case ‘text’: if (!is_string($input[$id])) { $this->_ret(false, 03, $id, ‘”‘ . ucfirst($id) . ‘” Must be text and it is not.’); break(3); //return array(false, $id . ‘ resulted in err03′); } break; default: trigger_error(’Invalid type provided to checkFields()’, E_USER_ERROR); } } } //return array(true, ‘err7 - Success’); if (!isset($this->lastError)) { $this->_ret(true, 07, NULL, ‘It was a success!’); } }
private function _ret($value, $code, $field_name, $description) { $error = array( ‘value’ => $value, ‘code’ => $code, ‘field’ => $field_name, ‘description’ => $description ); $this->lastError = $error; }
public function __get($id) { return $this->lastError[$id]; }}
The major revisions include:
- Is now object oriented.
- Does not return a value. To get information use $field_object->value/code/field/description or directly access the lastError array.
- Provides a description suitable to give straight to the client. The error information could be used to highlight fields which failed the requirements using ajax or another JavaScript technique.
So now the example I have earlier, redone would look like this:
$requirements = array( ‘date’ => ‘date’, ‘text’ => ‘name’, ‘email’ => ‘email’, ‘date, int, length=4′ => ‘yearofbirth’);$field_check = new CheckFields($requirements, $_POST);print_r($field_check->lastError);