Welcome Guest, Not a member yet? Register   Sign In
Error Messages in datamapper
#1

[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.
#2

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

[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?
#4

[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);
}
#5

[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.
#6

[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




Theme © iAndrew 2016 - Forum software by © MyBB