Here's a simple example that works for me.
In my model (client.php):
<?php defined('SYSPATH') or die('No direct script access.');
class Client_Model extends Model {
public $validation;
// This array is needed for validation
public $fields = array(
'clientName' => ''
);
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):
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Clients_Controller extends Base_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$content = new View('clients/read');
$content->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 .= '<br />Performing Validation<br />';
if ($client->create()) {
// Validation passed
answered 2010-03-17T19:50:38.063