I have a Registry where I store my request, validator, database and router objects. So for validation of my forms, I use a method in class MyForm, where the input fields are checked. When user input is incorrect, then Registry::getInstance()->validator->setErrors($errors) is called, so that errors are stored in the Registry and get available anywhere in code.
That's why i call Registry::getInstance()->validator->getErrors() in MyView.php
</head><body>
<form accept-charset="utf-8" action="newuser.php" method="post">
<?php
$errors = Registry::getInstance()->validator->getErrors();
if($errors) {
foreach($errors as $error) {
echo '<p>'. $error . '</p>';
}
}
?>
Though $MyForm->isValid() fills in the errors here in Registry::getInstance()->validator->setErrors($errors) correctly, clearly to see with var_dump(Registry::getInstance()), the validator object is null, when called in MyView.php.
So it still is the same object. But errortexts are gone. Why?
And where is the best place to store errortexts when validation in MyForm.php is false, so that i can grab the text in MyView.php?
Edit: this is the registry
class Registry {
private static $_instance = null;
private $_cache = array();
public static function getInstance()
{
if(null === self::$_instance) {
self::$_instance = new self;
}
return self::$_instance;
}
public function __set($index, $value) {
if(!empty($index) && is_string($index) && !array_key_exists($index, $this->_cache)) {
$this->_cache[$index] = $value;
}
else {
// Errorhandling here ...
}
}
public function __get($name)
{
if(array_key_exists($name, $this->_cache) && !empty($this->_cache[$name])) {
return $this->_cache[$name];
}
else {
return null;
}
}
public function __unset($index)
{
unset($this->_cache[$index]);
}
private function __construct() {}
private function __clone() { }
}
I guess, it has to do with the communication between the View and the Controller. Somehow it does not work. I don't know where to include the View, which is just html, not generated. This is my RegistrationController
class RegistrationController extends Controller {
public function execute()
{
$form = new RegistrationForm;
if($this->request->issetPost()) {
$posts = $this->request->getPosts();
if($form->isValid($posts)) {
// [..save .]
}
}
}
} I am still confused with the view. Like:
switch($page) {
case 'index':
// ..
break;
case 'news':
// ..
break;
case 'register_new_user':
$newuser = new RegistrationController;
$newuser->sign();
break;
} Where is the place to call a view, tell it that action was done with errors, so there is text in the Registry-Container for the view to show?