<p>You can get any validations errors on the category after you've attempted to save it with <a href="http://buildwithcraft.com/classreference/models/BaseModel#getAllErrors-detail"><code>CategoryModel::getAllErrors()</code></a> or <a href="http://www.yiiframework.com/doc/api/1.1/CModel#getErrors-detail"><code>\CModel::getErrors()</code></a>. </p>

<p>You're also setting the <code>groupId</code> on the Category's <a href="http://buildwithcraft.com/classreference/models/ContentModel"><code>ContentModel</code></a>, but this property is actually on the Category record itself as it sets the relationship directly.</p>

<pre><code>$category = new CategoryModel();
$category-&gt;groupId = $this-&gt;category_group_id;

$category-&gt;SetContentFromPost(array(
    'title' =&gt; $data[0],
    'colorCode' =&gt; $data[1],
    'colorPreview' =&gt; $data[2],
));

if (craft()-&gt;categories-&gt;saveCategory($category))
{
    return true;
}
else
{
    // dump the error messages
    Craft::dd($category-&gt;getAllErrors());
}
</code></pre>

CC BY-SA 3.0
2015-03-09T14:33:10.193Z
Hi Mike,  
thanks for the answer, this worked well to get all the errors.  
The title was causing the problem ("can't be null").  To fix it, I had to set the "title" property this ways :

    $category->getContent()->title = $data[0];

Is-it a normal behavior ?

Nicolas
Nicolas Binet
CC BY-SA 3.0
2015-03-09T15:01:13.503Z
Yes, the title and slug are defined on the ContentModel, so that different values can be stored for different locales.
Mike Pepper
CC BY-SA 3.0
2015-03-09T15:23:00.037Z
What if I had to set different values for different locales of the custom fields ?
Nicolas Binet
CC BY-SA 3.0
2015-03-09T10:29:25.787Z
2015-03-09T10:29:25.787Z
Mike Pepper
<p>I'm trying to create a category through the api to import a load of datas,
but I'm stuck, the category won't be saved, and the saveCategory() method just returns <em>false</em>  </p>

<pre><code>$category = new CategoryModel();
$category-&gt;setContentFromPost(array(
    'groupId'       =&gt;  $this-&gt;category_group_id,
    'title'         =&gt;  $data[0],
    'colorCode'     =&gt;  $data[1],
    'colorPreview'  =&gt;  $data[2]
));
$category-&gt;enabled = true;
$res = craft()-&gt;categories-&gt;saveCategory($category);
</code></pre>

<p>Can I get a glimpse of what's happening, or the error ?</p>

<p>Is there anything wrong in this code ?
Does anyone have a good example/documentation for this ?</p>

<p>Thanks for your help.</p>

<p>Nicolas</p>

<categories>
Saving Category through API
# Saving Category through API

(Asked by: username_0 on 2015-03-09)

I'm trying to create a category through the api to import a load of datas,
but I'm stuck, the category won't be saved, and the saveCategory() method just returns false 
$category = new CategoryModel();
$category->setContentFromPost(array(
    'groupId'       =>  $this->category_group_id,
    'title'         =>  $data[0],
    'colorCode'     =>  $data[1],
    'colorPreview'  =>  $data[2]
));
$category->enabled = true;
$res = craft()->categories->saveCategory($category);

Can I get a glimpse of what's happening, or the error ?
Is there anything wrong in this code ?
Does anyone have a good example/documentation for this ?
Thanks for your help.
Nicolas

**Tags:** categories

---

## 1 answers

### Answer by username_1 on 2015-03-09. Score: 5 | Accepted answer

You can get any validations errors on the category after you've attempted to save it with CategoryModel::getAllErrors() or \CModel::getErrors(). 
You're also setting the groupId on the Category's ContentModel, but this property is actually on the Category record itself as it sets the relationship directly.
$category = new CategoryModel();
$category->groupId = $this->category_group_id;

$category->SetContentFromPost(array(
    'title' => $data[0],
    'colorCode' => $data[1],
    'colorPreview' => $data[2],
));

if (craft()->categories->saveCategory($category))
{
    return true;
}
else
{
    // dump the error messages
    Craft::dd($category->getAllErrors());
}

> **Comments:**
> **username_0**: Hi Mike,   thanks for the answer, this worked well to get all the errors.   The title was causing the problem ("can't be null").  To fix it, I had to set the "title" property this ways :      $category->getContent()->title = $data[0];  Is-it a normal behavior ?  Nicolas  
> **username_1**: Yes, the title and slug are defined on the ContentModel, so that different values can be stored for different locales.  
> **username_0**: What if I had to set different values for different locales of the custom fields ?  

---

