Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-09T14:33:10.193Z
Answers.Text: 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
Answers.UserDisplayName: Nicolas Binet
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-09T15:01:13.503Z
Answers.Text: Yes, the title and slug are defined on the ContentModel, so that different values can be stored for different locales.
Answers.UserDisplayName: Mike Pepper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-09T15:23:00.037Z
Answers.Text: What if I had to set different values for different locales of the custom fields ?
Answers.UserDisplayName: Nicolas Binet
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-09T10:29:25.787Z
Answers.LastActivityDate: 2015-03-09T10:29:25.787Z
Answers.UserDisplayName: Mike Pepper
Body: <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>
  
Tags: <categories>
Title: Saving Category through API
ThreadText: # 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.
  
  username_0
  
  **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()`](http://buildwithcraft.com/classreference/models/BaseModel#getAllErrors-detail) or [`\CModel::getErrors()`](http://www.yiiframework.com/doc/api/1.1/CModel#getErrors-detail).
  
  You're also setting the `groupId` on the Category's [`ContentModel`](http://buildwithcraft.com/classreference/models/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 username_1,   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 ?  username_0  
  > **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 ?  
  
  ---
  
  