I'm struggling with the proper organisation of a (MVC) Controller for my validation of a multipaged form. The problem is that I not only have to check whether the user input is existant at all, but I have to match it with different databases (depending on the field), too. I also need that database-data that results from the user-input for different view-options.
It's unclear for me where I should put that validation at all. I would like to put not too detailed things into the controller, but at the same time I don't like models using each other (without the controller).
Here's a basic example from a controller, the steps stand for different stages/pages of the form:
// GET VARIABLES FROM $_POST + $_GET
private function make_environment()
{
// PUT ALL VARIABLES INTO ARRAY
$vars = array();
if(!empty($_GET)) { $values = array_merge($vars,$_GET); }
if(!empty($_POST)) { $values = array_merge($vars,$_POST); }
// PUT ALLOWED VARIABLES INTO PROPERTY
foreach($this->properties as $property)
{
if(isset($values[$property]))
{
$this->properties[$property] = htmlspecialchars(trim($values[$property]));
}
}
}
// HANDLE DATA
// PRODUCES DATA FOR VIEW
private function set_data()
{
$data = '';
// CHOOSE DATA-OBJECT AND DATA-HANDLER
switch($this->properties['step'])
{
case 1:
// DATA HANDLER
$handler = new calendar($this->properties);
$data['calendar'] = $handler->return_data();
break;
case 2:
// DATA HANDLER
$handler = new form($this->properties);
$data['form'] = $handler->return_data();
break;
}
return $data;
}
// CREATE OR UPDATE VIEW
private function run_view($data)
{
new view('header','');
switch($this->properties['step'])
{
default:
new view('chooser','');
break;
case 1:
new view('calendar',$data['calendar'],$this->properties);
break;
case 2:
new view('form',$data['form'],$this->properties);
break;
}
new view('footer','');
}
At the moment the different 'handlers' (in set_data()
) are querying the database, but prior to that I'd need a check whether the required fields have been submitted and whether those fields are valid (whether they exist in the database). I don't really know where to put that kind of validation. Propably in a separate model, but then I'd have to query the DB there and in the data-handler again. Maybe you got an idea ?!
No comments:
Post a Comment