[eluser]freshfutures[/eluser]
Hi,
I've done a quick search on this topic and found nothing so I thought I'd post my problem here and see if anyone knows a solution.
I'm using Codeigniter 2 and DataMapper 1.81 and I've got two problems:
1) I'm trying to display form validation errors when a form has been submitted. The validation is done in the model, however I'm not getting proper error strings like 'Name' is a required field, instead I get just 'required'.
2) I'm stumped as to how I can repopulate the form fields a user has filled in when the page is reloaded after validation fails.
My code is attached below:
Controller
Code:
if (!empty($_POST)) {
$section = new Section_model();
$section->name = $this->input->post('name');
$section->description = $this->input->post('description');
$section->validate();
if($section->valid) {
// save object and return to listings
$section->save();
$this->template->set_message("Created the section '$section->name'", 'success');
redirect('admin/sections/listings');
} else {
// display validation error messages
$this->template->set('errors', $section->error->all);
$this->template->render();
}
} else {
$this->template->render();
}
Model
Code:
class Section_model extends DataMapper {
var $table = 'sections';
var $validation = array(
'name' => array(
'label' => 'Section Name',
'rules' => array('required')
),
'description' => array(
'label' => 'Section Description',
'rules' => array('required', 'min_length' => 3)
)
);
function __construct($id = NULL)
{
parent::__construct($id);
}
function post_model_init($from_cache = FALSE)
{
}
}
$section->error->all returns an array:
Code:
array(2) { ["name"]=> string(15) "required" ["description"]=> string(15) "required" }
I'm completely stumped so any help would be greatly appreciated!