I find examples and tutorials about models and about validation. And I places that say the validation (or most of it at least) should be in the model, which I agree with. But I
Here's a simple example that works for me.
In my model (client.php):
''
);
public function __construct() {
// load database library into $this->db (can be omitted if not required)
parent::__construct();
$this->validation = new Validation($_POST);
$this->validation->pre_filter('trim','clientName');
$this->validation->add_rules('clientName','required');
}
public function create() {
return $this->validation->validate();
}
// This might go in base Model class
public function getFormValues() {
return arr::overwrite($this->fields, $this->validation->as_array());
}
// This might go in base Model class
public function getValidationErrors() {
return arr::overwrite($this->fields, $this->validation->errors('form_errors'));
}
}
?>
In my controller (clients.php):
foobar = 'bob.';
$this->template->content = $content;
$this->template->render(TRUE);
}
/* A new user signs up for an account. */
public function signup() {
$content = new View('clients/create');
$post = $this->input->post();
$client = new Client_Model;
if (!empty($post) && $this->isPostRequest()) {
$content->message = 'You submitted the form, '.$this->input->post('clientName');
$content->message .= '
Performing Validation
';
if ($client->create()) {
// Validation passed
$content->message .= 'Validation passed';
} else {
// Validation failed
$content->message .= 'Validation failed';
}
} else {
$content->message = 'You did not submit the form.';
}
$contnet->message .= '
';
print_r ($client->getFormValues());
print_r ($client->getValidationErrors());
$this->template->content = $content;
$this->template->render(TRUE);
}
}
?>
In my i18n file (form_errors.php):
$lang = Array (
'clientName' => Array (
'required' => 'The Client Name field is required.'
)
);