Welcome Guest, Not a member yet? Register   Sign In
DataMapper, Validation, Crud issues
#1

[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!


Messages In This Thread
DataMapper, Validation, Crud issues - by El Forum - 07-11-2011, 04:50 PM
DataMapper, Validation, Crud issues - by El Forum - 07-11-2011, 11:54 PM
DataMapper, Validation, Crud issues - by El Forum - 07-12-2011, 03:07 AM
DataMapper, Validation, Crud issues - by El Forum - 07-12-2011, 04:37 AM
DataMapper, Validation, Crud issues - by El Forum - 07-12-2011, 06:28 AM
DataMapper, Validation, Crud issues - by El Forum - 07-12-2011, 09:33 AM
DataMapper, Validation, Crud issues - by El Forum - 07-23-2011, 04:48 PM
DataMapper, Validation, Crud issues - by El Forum - 07-24-2011, 03:01 AM
DataMapper, Validation, Crud issues - by El Forum - 07-29-2011, 06:46 PM
DataMapper, Validation, Crud issues - by El Forum - 07-30-2011, 02:49 AM



Theme © iAndrew 2016 - Forum software by © MyBB