CodeIgniter Forums
Error Messages in datamapper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Error Messages in datamapper (/showthread.php?tid=41186)



Error Messages in datamapper - El Forum - 04-30-2011

[eluser]johnmerlino[/eluser]
Hey all,

I read this in datamapper documentation:

Code:
If any of the field values fail validation, the object will have its error property populated. You can view loop through and show each error in the error's all list, show the specific error for each field, or show all errors in one string. For example:

foreach ($object->error->all as $e)
{
    echo $e . "<br />";
}

Here's the problem. It doesn't tell you what is stored in the variable $object. If I have this in a Category model:

Code:
var $validation = array(
                'controller' => array(
                    'label' => 'Enter Category Name',
                    'rules' => array('required','min_length' => 5, 'max_length' => 10, 'alpha_dash')
                )
          );

And my new method of categories controller just does a get to render a form, I am not sure what it wants that $object variable to store in it.

Thanks for response.


Error Messages in datamapper - El Forum - 04-30-2011

[eluser]WanWizard[/eluser]
$object is your datamapper object.


Error Messages in datamapper - El Forum - 05-01-2011

[eluser]johnmerlino[/eluser]
Thanks for response. I am not sure when you say datamapper object, are you referring to object itself, an instance of it, or an instance of the subclass object?


Error Messages in datamapper - El Forum - 05-02-2011

[eluser]WanWizard[/eluser]
In the documentation, I use $object whenever I mean: an instance of a Datamapper model class.

So
Code:
$object = new Model();
$object->field = 'invalid';

if ( ! $object->save() )
{
    var_dump($object->error);
}



Error Messages in datamapper - El Forum - 05-07-2011

[eluser]johnmerlino[/eluser]
Thanks for response.


This though:

Code:
$this->session->set_flashdata('flash_message',serialize($subcategory->error));
                redirect("blogs/{$blogger->vanityurl->get()->url}/categories/news");


gives this:

Code:
O:15:"DM_Error_Object":3:{s:3:"all";a:1:{s:10:"controller";s:77:"
The Enter Category Name field must be at least 5 characters in length.

";}s:6:"string";s:77:"
The Enter Category Name field must be at least 5 characters in length.

";s:10:"controller";s:77:"
The Enter Category Name field must be at least 5 characters in length.

";}

Rather than this:

Code:
The Enter Category Name field must be at least 5 characters in length.

The Enter Category Name field must be at least 5 characters in length.

The Enter Category Name field must be at least 5 characters in length.



Error Messages in datamapper - El Forum - 05-07-2011

[eluser]WanWizard[/eluser]
Quite logical, you're serializing an object. That's why I suggested to var_dump() the $object->error, so you could see what it was.

If you want the array of messages, you need to use
Code:
$subcategory->error->all

Or as a string:
Code:
$subcategory->error->string