We have a change profile page where members can choose out of the users table who of the other members is their mother (or father).

Below my (simplified) user profile form

<form method="post" accept-charset="UTF-8" enctype="multipart/form-data">
{{ getCsrfInput() }}
<input type="hidden" name="action" value="users/saveUser">
<input type="hidden" name="redirect" value="leden/wijzig-profiel?m=updated">
<input type="hidden" name="userId" value="{{ currentUser.id }}">
<input type="hidden" name="fields[mother][]" value="">

<label for="mother">{{ 'mother'|t }}</label>
<select name="fields[mother][]" >
    <option value="">There is no mother</option>
    {% for mother in mothers %}
    <option value="{{ mother.id }}">{{ mother.name }}</option>
    {% endfor %}
    {% for user in users %}
    <option value="{{ user.id }}">{{ user.name}}</option>
    {% endfor %}    
<input type="submit" value="{{ 'Save'|t }}" name="proceed" >

I've tried to empty the field mother but that results in a CDbException error.

CDbCommand faalde tijdens het uitvoeren van volgend SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`familywebsite`.`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), (:row1_col0, :row1_col1, NULL, :row1_col3, :row1_col4, :row1_col5, :row1_col6, :row1_col7). Bound with :row0_col0='136', :row0_col1='1', :row0_col3='', :row0_col4=1, :row0_col5='2016-10-08 09:54:35', :row0_col6='2016-10-08 09:54:35', :row0_col7='e4211e99-5bbd-46c1-b703-c44e82bc4667', :row1_col0='136', :row1_col1='1', :row1_col3='2409', :row1_col4=2, :row1_col5='2016-10-08 09:54:35', :row1_col6='2016-10-08 09:54:35', :row1_col7='c0fb9927-89f1-4993-ba26-bc47a63bdc50'

This I can understand because the mother is a relation of the user, and by clearing the mother field value, it probably does not delete the relation and it will give and error like it does.

So question is how to delete the relation with mother field?

1 Answer 1


I believe you're getting that error because you're supplying an array ([]) for the mother field and the only value in that array would be an empty string ("") if they select the There is no mother option. So what Craft is attempting to save as a relation is [""]. Instead of an array of integer ID numbers, it's getting an empty string which isn't a valid value.

Even if you remove the value attribute of the option tag for the There is no mother option, the field will use the inner text of the tag as the value, so this does not solve your problem either.

One suggestion would be to use radio buttons instead and do the following:

<label><input type="radio" name="" value=""> There is no mother</label>
{% for mother in mothers %}
    <label><input type="radio" name="fields[mother][]" value="{{ mother.id }}"> {{ mother.name }}</label>
{% endfor %}
{% for user in users %}
    <label><input type="radio" name="fields[mother][]" value="{{ user.id }}"> {{ user.name }}</label>
{% endfor %}

This will keep the field empty if someone selects the There is no mother option.

  • Hello Stephen, thanks for your answer. I'm affraid that radiobutton won't work in our case, because we have 600 users to choose from that could be a mother. But l want to ask it a little different. Let's say user 300 is my mother. What input value should I send to remove user 300 as my mother. (So forget about select field) Oct 10, 2016 at 18:21
  • 1
    Ok figured it out. I can only send value="" to "fields[mother]" and not to fields[mother][] because then it should be an array as you point out. I will use jQuery to solve this. Oct 10, 2016 at 19:17
  • Glad I could help @JohnStevens but looks like you got there on your own in the end :-) Oct 11, 2016 at 9:09
  • Coming from EE, so there is still a lot to learn for me :) Oct 11, 2016 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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