<p>This is due to the way Craft manages custom fields at the moment (but has been “fixed” for Craft 3). Whenever you save an element, you need to make sure all of the field values are in the same format they would be if they had come from an Edit Entry form submission. Here’s another post that describes what that looks like: <a href="https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin/1218#1218">How to save a matrix content of a new entry in my plugin?</a></p>

CC BY-SA 3.0
2015-10-16T13:41:51.220Z
2015-10-16T13:41:51.220Z
2017-04-13T12:51:00.200Z
Brandon Kelly
<p>I have a task that is used to collate relationship data for a specific entry and save it as JSON to a text field in that entry. Everything is working fine, except for the saving of the entry which fails because the required Matrix / Assets fields in the entry are being read as blank. I've checked to make sure that the entries have all required fields completed, but the errors still persist.</p>

<p>The entry saving code in the task:</p>

<pre><code>$entry-&gt;getContent()-&gt;setAttribute('latLngs', json_encode($latLngs));

craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

<p>The errors being returned:</p>

<pre><code>[_errors:CModel:private] =&gt; Array
(
    [gallery] =&gt; Array
    (
        [0] =&gt; Gallery cannot be blank.
    )
    [standardContent] =&gt; Array
    (
        [0] =&gt; Standard Content cannot be blank.
    )
)
</code></pre>

<p>Where <code>gallery</code> is an Assets field, and <code>standardContent</code> is a Matrix field. All other required fields seem to be working fine with no errors.</p>

<p>Is this to do with the way I'm saving the entries, or something else?</p>

<p>Thanks!</p>

<assets><matrix><entry><entrymodel><tasks>
Entry saving in task fails due to required assets/matrix fields being read as blank, even through they are not
# Entry saving in task fails due to required assets/matrix fields being read as blank, even through they are not

(Asked by: username_0 on 2015-10-13)

I have a task that is used to collate relationship data for a specific entry and save it as JSON to a text field in that entry. Everything is working fine, except for the saving of the entry which fails because the required Matrix / Assets fields in the entry are being read as blank. I've checked to make sure that the entries have all required fields completed, but the errors still persist.
The entry saving code in the task:
$entry->getContent()->setAttribute('latLngs', json_encode($latLngs));

craft()->entries->saveEntry($entry);

The errors being returned:
[_errors:CModel:private] => Array
(
    [gallery] => Array
    (
        [0] => Gallery cannot be blank.
    )
    [standardContent] => Array
    (
        [0] => Standard Content cannot be blank.
    )
)

Where gallery is an Assets field, and standardContent is a Matrix field. All other required fields seem to be working fine with no errors.
Is this to do with the way I'm saving the entries, or something else?
Thanks!

**Tags:** assets, matrix, entry, entrymodel, tasks

---

## 1 answers

### Answer by username_1 on 2015-10-16. Score: 1 | Accepted answer

This is due to the way Craft manages custom fields at the moment (but has been “fixed” for Craft 3). Whenever you save an element, you need to make sure all of the field values are in the same format they would be if they had come from an Edit Entry form submission. Here’s another post that describes what that looks like: [How to save a matrix content of a new entry in my plugin?](https://craftcms.stackexchange.com/questions/1101/how-to-save-a-matrix-content-of-a-new-entry-in-my-plugin/1218#1218)


---

