

<p><a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getContent-detail" rel="nofollow noreferrer"><code>getContent()</code></a> will return the element’s <a href="http://buildwithcraft.com/classreference/models/ContentModel" rel="nofollow noreferrer">ContentModel</a>, <strong>NOT</strong> <code>getContent()-&gt;merchant</code>, which is just an attribute of the ContentModel—sorry for the confusion.</p>

<p>I’ve just posted a separate Q&amp;A fully explaining what the ContentModel is used for here:</p>

<p><strong><a href="https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust">What is getContent() / the ContentModel, and how do I use it to get and set custom field values?</a></strong></p>

<p>What you are experiencing is actually a slight bug in Craft’s validation logic. Relational fields have a feature where their data is not actually required to be in POST when saving an entry. If that’s the case (if their value in the ContentModel ends up just being <code>null</code>), then they will simply not make any changes to the element’s relationships - the field value will be left untouched.</p>

<p>However the ContentModel is not aware of that feature, and it’s the ContentModel that is currently in charge of enforcing Required Field validation. So, when it gets to your relational field, and finds that its value is <code>null</code>, it gives you a validation error rather than letting it slide.</p>

<p>So, you have two workaround options, if you want to keep the data the same, but need the field to be required:</p>

<ol>
<li><p>You can actually set the value based on the current value:</p>

<pre class="lang-php prettyprint-override"><code>$entry-&gt;setContentFromPost(array(
    // ...
    'merchant' =&gt; $entry-&gt;merchant-&gt;ids(),
));

craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre></li>
<li><p>If all you’re doing is changing custom field values (nothing entry-specific) and you’re comfortable skipping custom field validation altogether, you can just call <a href="http://buildwithcraft.com/classreference/services/ContentService#saveContent-detail" rel="nofollow noreferrer">ContentService::saveContent()</a> directly, and pass in <code>false</code> to its second argument which tells it to skip validation:</p>

<pre class="lang-php prettyprint-override"><code>$entry-&gt;setContentFromPost(array(
    // ...
    'merchant' =&gt; null, // Tells the relational field to keep the old value
));

craft()-&gt;content-&gt;saveContent($entry, false);
</code></pre></li>
</ol>

CC BY-SA 3.0
2016-04-21T08:16:46.693Z
Just experienced the same behavior, the workaround does the job thanks! are there any developments on this behavior
Jacob
CC BY-SA 3.0
2016-04-21T13:51:16.987Z
@Jacob Yes we’ve made this not-awkward in Craft 3 :)
Brandon Kelly
CC BY-SA 3.0
2014-10-31T20:16:26.777Z
2014-10-31T20:16:26.777Z
2017-04-13T12:51:00.303Z
Brandon Kelly
<p>I have a custom plugin that's saving an entry successfully. While saving the entry I'm setting our "merchant" field (an entries field, limit=1) like so:</p>

<pre><code>&lt;?php
# ...
$entry = newEntryModel();
# set the sectionId, typeId, authorId, and some other fields...
$entry-&gt;getContent()-&gt;merchant = (isset($data['merchant']) ? [$data['merchant']-&gt;id] : null; // $data['merchant'] is an EntryModel
</code></pre>

<p>This appears to be fine in the CP. I can see and edit the related entry.</p>

<p>However later in the application I have to update this entry and when updating the entry, I'm only updating a single field. While debugging the EntryModel that I'm trying to save I see that if I var_dump $entry->merchant I get an ElementCriteriaModel yet when I dump $entry->getContent->merchant I get 'null'.</p>

<p>My log shows the following error:</p>

<blockquote>
  <p>2014/10/31 18:25:46 [warning] [application]
  Craft\ContentModel->merchant failed validation: Merchant cannot be
  blank.</p>
</blockquote>

<p>According to <a href="https://twitter.com/pixelandtonic/status/528256177327452161" rel="nofollow">this tweet</a> it seems like dumping $entry->getContent()->merchant should be returning a ContentModel instead of null.</p>

<p>Any ideas?</p>

<p>EDIT: I've confirmed that everything works as expected if the field is NOT marked as required. However, this can't really be a solution - I need to make sure that the client cannot leave this field blank.</p>

<plugin-development><relations><elementcriteriamodel><entrymodel><contentmodel>
Can someone help me identify why my entry is invalidating?
# Can someone help me identify why my entry is invalidating?

(Asked by: username_0 on 2014-10-31)

I have a custom plugin that's saving an entry successfully. While saving the entry I'm setting our "merchant" field (an entries field, limit=1) like so:
getContent()->merchant = (isset($data['merchant']) ? [$data['merchant']->id] : null; // $data['merchant'] is an EntryModel

This appears to be fine in the CP. I can see and edit the related entry.
However later in the application I have to update this entry and when updating the entry, I'm only updating a single field. While debugging the EntryModel that I'm trying to save I see that if I var_dump $entry->merchant I get an ElementCriteriaModel yet when I dump $entry->getContent->merchant I get 'null'.
My log shows the following error:

2014/10/31 18:25:46 [warning] [application]
  Craft\ContentModel->merchant failed validation: Merchant cannot be
  blank.

According to this tweet it seems like dumping $entry->getContent()->merchant should be returning a ContentModel instead of null.
Any ideas?
EDIT: I've confirmed that everything works as expected if the field is NOT marked as required. However, this can't really be a solution - I need to make sure that the client cannot leave this field blank.

**Tags:** plugin-development, relations, elementcriteriamodel, entrymodel, contentmodel


---

## 1 answers

### Answer by username_1 on 2014-10-31. Score: 4 | Accepted answer

getContent() will return the element’s ContentModel, NOT getContent()->merchant, which is just an attribute of the ContentModel—sorry for the confusion.
I’ve just posted a separate Q&A fully explaining what the ContentModel is used for here:
What is getContent() / the ContentModel, and how do I use it to get and set custom field values?
What you are experiencing is actually a slight bug in Craft’s validation logic. Relational fields have a feature where their data is not actually required to be in POST when saving an entry. If that’s the case (if their value in the ContentModel ends up just being null), then they will simply not make any changes to the element’s relationships - the field value will be left untouched.
However the ContentModel is not aware of that feature, and it’s the ContentModel that is currently in charge of enforcing Required Field validation. So, when it gets to your relational field, and finds that its value is null, it gives you a validation error rather than letting it slide.
So, you have two workaround options, if you want to keep the data the same, but need the field to be required:

You can actually set the value based on the current value:
$entry->setContentFromPost(array(
    // ...
    'merchant' => $entry->merchant->ids(),
));

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

If all you’re doing is changing custom field values (nothing entry-specific) and you’re comfortable skipping custom field validation altogether, you can just call ContentService::saveContent() directly, and pass in false to its second argument which tells it to skip validation:
$entry->setContentFromPost(array(
    // ...
    'merchant' => null, // Tells the relational field to keep the old value
));

craft()->content->saveContent($entry, false);

> **Comments:**
> **username_2**: Just experienced the same behavior, the workaround does the job thanks! are there any developments on this behavior  
> **username_1**: @username_2 Yes we’ve made this not-awkward in Craft 3 :)  

---

