1

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:

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

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!

  • 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 – Robin Schambach Nov 7 '17 at 16:20
  • 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 – Robin Schambach Nov 7 '17 at 19:12

Your Answer

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

Browse other questions tagged or ask your own question.