<p>I have successfully implemented a front-end form that allows the user to add new "blocks" to a certain matrix field. This matrix field has one text field, two date fields, and one entry field that links to one specific entry type: <code>sites</code>.</p>

<p><strong>How can I add an option to the dropdown list that would allow the user to "ignore" this field as if they left it alone when adding data in the admin area?</strong></p>

<p>Here is what I have tried:</p>

<pre><code>&lt;select name="fields[scriptLots][new1][fields][lotSite][]"&gt;
    &lt;option value=""&gt;No Site&lt;/option&gt;
    {% for site in sites.order('title') %}
        &lt;option value="{{site.id}}"&gt;{{site.title}}&lt;/option&gt;
    {% endfor %}
&lt;/select&gt;
</code></pre>

<p>I figured just adding an option tag ahead of the <code>for</code> loop with no value would do the trick. However this just responds in a major error:</p>

<pre><code>CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dsi_craft_vetscripts`.`craft_relations`, CONSTRAINT `craft_relations_targetId_fk` FOREIGN KEY (`targetId`) REFERENCES `craft_elements` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `craft_relations` (`fieldId`, `sourceId`, `sourceLocale`, `targetId`, `sortOrder`, `dateCreated`, `dateUpdated`, `uid`) VALUES (:row0_col0, :row0_col1, NULL, :row0_col3, :row0_col4, :row0_col5, :row0_col6, :row0_col7). Bound with :row0_col0='54', :row0_col1='3382', :row0_col3='', :row0_col4=1, :row0_col5='2017-11-07 15:26:00', :row0_col6='2017-11-07 15:26:00', :row0_col7='06895bfe-d902-49f6-a085-bd080f68cdef'
</code></pre>

<p>So that empty value attribute seems to be breaking something. How can I add a null option?</p>

<p>Thanks!</p>

CC BY-SA 3.0
2017-11-07T16:20:18.487Z
You can't do that. Every entry must be linked to a site. You can't include a "null" option. You would have to alter the table and change the default query since they are related to the siteId. If an entry is not related to a site you would not be able to display it in the backend cp. You can only add a default site to it
Robin Schambach
CC BY-SA 3.0
2017-11-07T19:12:00.820Z
What you could do is creating another site as your "null" value and hide this one in your cp via permissions or via a plugin. But those entries won't make it to the frontend unless you specify it in twig
Robin Schambach
<matrix><entries-field><frontend-form>
Front end entry dropdown with null option
# Front end entry dropdown with null option

(Asked by: username_0 on 2017-11-07)

I have successfully implemented a front-end form that allows the user to add new "blocks" to a certain matrix field. This matrix field has one text field, two date fields, and one entry field that links to one specific entry type: sites.
How can I add an option to the dropdown list that would allow the user to "ignore" this field as if they left it alone when adding data in the admin area?
Here is what I have tried:

    No Site
    {% for site in sites.order('title') %}
        {{site.title}}
    {% endfor %}


I figured just adding an option tag ahead of the for loop with no value would do the trick. However this just responds in a major error:
CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dsi_craft_vetscripts`.`craft_relations`, CONSTRAINT `craft_relations_targetId_fk` FOREIGN KEY (`targetId`) REFERENCES `craft_elements` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `craft_relations` (`fieldId`, `sourceId`, `sourceLocale`, `targetId`, `sortOrder`, `dateCreated`, `dateUpdated`, `uid`) VALUES (:row0_col0, :row0_col1, NULL, :row0_col3, :row0_col4, :row0_col5, :row0_col6, :row0_col7). Bound with :row0_col0='54', :row0_col1='3382', :row0_col3='', :row0_col4=1, :row0_col5='2017-11-07 15:26:00', :row0_col6='2017-11-07 15:26:00', :row0_col7='06895bfe-d902-49f6-a085-bd080f68cdef'

So that empty value attribute seems to be breaking something. How can I add a null option?
Thanks!

**Tags:** matrix, entries-field, frontend-form

> **Comments:**
> **username_1**: You can't do that. Every entry must be linked to a site. You can't include a "null" option. You would have to alter the table and change the default query since they are related to the siteId. If an entry is not related to a site you would not be able to display it in the backend cp. You can only add a default site to it  
> **username_1**: What you could do is creating another site as your "null" value and hide this one in your cp via permissions or via a plugin. But those entries won't make it to the frontend unless you specify it in twig  

---

## 0 answers

