<p>It looks like it's doing what it should. When you set a field to 'required', you are basically telling the system to not let the entry be saved if you haven't filled out the field you've set 'required' on. That's what this error saying (albeit at a database level). </p>

<p>Two quick solutions as I see it...</p>

<ol>
<li>Always make sure you have a default value that can be used for this
field. So you aren't violating the 'required' setting.</li>
<li>Remove the 'required' option from the field in the CP.</li>
</ol>

<p>EDIT: In the Plugin settings in the CP, have you turned on the <code>Validate Entry</code> lightswitch for the section you are adding to on the front end? Only I've just had a quick play around with the plugin myself, and I get a validation error when I don't submit an entryId for the related section on the front end form. So it looks as though it's working as expected.</p>

<p>I used this to get the errors...</p>

<pre><code>{%for error in guestEntry.getAllErrors %}
    {{ error }}
{% endfor %}
</code></pre>

<p>I also just used the default config file <code>guestEntries.php</code> as suggested in the README...</p>

<pre><code>&lt;?php

return array(
    'entryVariable' =&gt; 'guestEntry',
);
</code></pre>

CC BY-SA 3.0
2016-04-08T11:50:24.170Z
Thanks, but why is the error not handled over to the getErrors function, if i make a simple text field required it will have an error like 'field email cannot be empty' why is this different with a relation field. For now i will stick with indeed, a simple fix, a default value
Jacob
CC BY-SA 3.0
2016-04-08T12:35:03.430Z
I'm not the developer of the plugin, so I'm afraid I can't answer that. Maybe, you could open an issue on the Github page https://github.com/pixelandtonic/GuestEntries/issues for the plugin?
Steve Holland
CC BY-SA 3.0
2016-04-08T13:10:59.670Z
I've just updated my answer with a little more info, it does look like it works as you were hoping though.
Steve Holland
CC BY-SA 3.0
2016-04-08T11:03:22.827Z
2016-04-08T13:09:41.367Z
2016-04-08T13:09:41.367Z
Steve Holland
<p>I've run into this same issue without the field being "required", so I don't think that is the problem.</p>

<p>I can recreate the problem by submitting a relationship input with an empty string as a value (e.g. I have a <code>fields[myEntriesField]</code> <code>&lt;select&gt;</code> input with an empty "Choose One" value.</p>

<p>The same error can be triggered programmatically by passing a <code>null</code> field value to <code>$entry-&gt;setContentFromPost</code>.</p>

<p>I'm using a workaround, but the core issue seems like a bug to me. This is within <code>guestEntries.beforeSave</code>:</p>

<pre><code>$entry = $event-&gt;params['entry'];

// Seemingly, when the `fields.product` post data has an array
// with an empty string (like it does from an unselected &lt;select&gt;),
// we get a CDbException SQLSTATE[23000]: Integrity constraint violation error.
// The same thing seems to happen when setContentFromPost receives null on a
// relation field, so we'll coalesce to empty string.
$product = array_filter(craft()-&gt;request-&gt;getPost('fields.product'));
$entry-&gt;setContentFromPost([
  'product' =&gt; $product ?? '',
]);
</code></pre>

<p>Note: posted a bug report here: <a href="https://github.com/pixelandtonic/GuestEntries/issues/14" rel="nofollow">https://github.com/pixelandtonic/GuestEntries/issues/14</a></p>

CC BY-SA 3.0
2016-10-12T11:44:15.727Z
2016-10-12T11:56:24.343Z
2016-10-12T11:56:24.343Z
Tim Kelty
<p>I am trying to save a entry with the Guest Entries plugin.</p>

<p>The entry has a custom field 'office' thats set to 'entry' a relation to section 'offices'.</p>

<pre><code>&lt;fieldset class="form-fieldset row-spacing-2"&gt;
    &lt;select class="form-input form-select" name="fields[office][]"&gt;
        &lt;option&gt;Select office&lt;/option&gt;
        {% for office in craft.entries.section('offices') %}
            &lt;option value="{{ office.id }}"&gt;{{ office.title }}&lt;/option&gt;
        {% endfor %}    
    &lt;/select&gt;
&lt;/fieldset&gt;
</code></pre>

<p>The field is set to required in the CP. When I save it with the field set it works, but when empty it throws this error:</p>

<pre><code>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
</code></pre>

<p>Can this error be caught or am i doing something wrong here?</p>

<p>Thanks in advance!</p>

<relations><form><plugin-guestentries>
Empty relation field validation failed with Guest Entries plugin
# Empty relation field validation failed with Guest Entries plugin

(Asked by: username_0 on 2016-04-08)

I am trying to save a entry with the Guest Entries plugin.
The entry has a custom field 'office' thats set to 'entry' a relation to section 'offices'.

    
        Select office
        {% for office in craft.entries.section('offices') %}
            {{ office.title }}
        {% endfor %}    
    


The field is set to required in the CP. When I save it with the field set it works, but when empty it throws this error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

Can this error be caught or am i doing something wrong here?
Thanks in advance!

**Tags:** relations, form, plugin-guestentries


---

## 2 answers

### Answer by username_1 on 2016-04-08. Score: 2

It looks like it's doing what it should. When you set a field to 'required', you are basically telling the system to not let the entry be saved if you haven't filled out the field you've set 'required' on. That's what this error saying (albeit at a database level). 
Two quick solutions as I see it...

Always make sure you have a default value that can be used for this
field. So you aren't violating the 'required' setting.
Remove the 'required' option from the field in the CP.

EDIT: In the Plugin settings in the CP, have you turned on the Validate Entry lightswitch for the section you are adding to on the front end? Only I've just had a quick play around with the plugin myself, and I get a validation error when I don't submit an entryId for the related section on the front end form. So it looks as though it's working as expected.
I used this to get the errors...
{%for error in guestEntry.getAllErrors %}
    {{ error }}
{% endfor %}

I also just used the default config file guestEntries.php as suggested in the README...
 'guestEntry',
);

> **Comments:**
> **username_0**: Thanks, but why is the error not handled over to the getErrors function, if i make a simple text field required it will have an error like 'field email cannot be empty' why is this different with a relation field. For now i will stick with indeed, a simple fix, a default value  
> **username_1**: I'm not the developer of the plugin, so I'm afraid I can't answer that. Maybe, you could open an issue on the Github page https://github.com/pixelandtonic/GuestEntries/issues for the plugin?  
> **username_1**: I've just updated my answer with a little more info, it does look like it works as you were hoping though.  

---

### Answer by username_2 on 2016-10-12. Score: 1

I've run into this same issue without the field being "required", so I don't think that is the problem.
I can recreate the problem by submitting a relationship input with an empty string as a value (e.g. I have a fields[myEntriesField]  input with an empty "Choose One" value.
The same error can be triggered programmatically by passing a null field value to $entry->setContentFromPost.
I'm using a workaround, but the core issue seems like a bug to me. This is within guestEntries.beforeSave:
$entry = $event->params['entry'];

// Seemingly, when the `fields.product` post data has an array
// with an empty string (like it does from an unselected ),
// we get a CDbException SQLSTATE[23000]: Integrity constraint violation error.
// The same thing seems to happen when setContentFromPost receives null on a
// relation field, so we'll coalesce to empty string.
$product = array_filter(craft()->request->getPost('fields.product'));
$entry->setContentFromPost([
  'product' => $product ?? '',
]);

Note: posted a bug report here: https://github.com/pixelandtonic/GuestEntries/issues/14


---

