1

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'.

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

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!

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...

  1. Always make sure you have a default value that can be used for this field. So you aren't violating the 'required' setting.
  2. 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...

<?php

return array(
    'entryVariable' => 'guestEntry',
);
|improve this answer|||||
  • 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 Apr 8 '16 at 11:50
  • 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 github.com/pixelandtonic/GuestEntries/issues for the plugin? – Steve Holland Apr 8 '16 at 12:35
  • I've just updated my answer with a little more info, it does look like it works as you were hoping though. – Steve Holland Apr 8 '16 at 13:10
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] <select> 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 <select>),
// 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

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.