Hello,

I'm not sure whether this is a bug or I'm just missing something.

We've developed a content type and packaged it into code using Features. Our content type contains a field 'field_test' of type "list_text" with widget "options_buttons" and unlimited cardinality. We've also set one of values in that field to be checked by default.

Now, in our tests, we want to uncheck all checkboxes and save empty 'field_test'. Unfortunately, our test fails - despite the fact that we have set values for all checboxes to NULL it saves the default value.

Let's explain it with some code snippets (note: full code is attached). Our tests simply try to uncheck the fields:

   $edit = array();
    $edit['title'] = $this->randomName(8);
    $edit["field_test[und][Option1]"] = FALSE;
    $edit["field_test[und][Option2]"] = FALSE;

    $this->drupalPost('node/add/test', $edit, t('Save'));
    $this->assertText(t('Test @title has been created.', array('@title' => $edit['title'])));

    $result = db_query("SELECT n.nid FROM {node} n WHERE n.title = :title", array(":title"=> $edit['title']));
    $nid = $result->fetchField();
    $this->drupalGet("node/{$nid}/edit");
    $this->assertResponse(200, 'Check the edit page of created node.');
    
    $this->assertNoFieldChecked("edit-field-test-und-option1", "Option1"); // this line fails
    $this->assertNoFieldChecked("edit-field-test-und-option2", "Option2");

Now, this code fails because Option1 is checked (despite our attempt to uncheck that by setting value to FALSE).

Our field definition is as follows:

 // Exported field_base: 'field_test'
  $field_bases['field_test'] = array(
    'cardinality' => -1,
    'field_name' => 'field_test',
    'module' => 'list',
    'settings' => array(
      'allowed_values' => array(
        'Option1' => 'Option1',
        'Option2' => 'Option2',
      ),
  );
// Exported field_instance: 'node-test-field_test'
  $field_instances['node-test-field_test'] = array(
    'bundle' => 'test',
    'default_value' => array(
      0 => array(
        'value' => 'Option1',
      ),
    ),
    'field_name' => 'field_test',
    'label' => 'Test',
    'required' => 0,
    'widget' => array(
      'active' => 1,
      'module' => 'options',
      'type' => 'options_buttons',
    ),
  );

Please also see attached Proof-Of-Concept module that shows the issue.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lklimek’s picture

dcam’s picture

Version: 7.28 » 7.x-dev
Component: simpletest.module » field system
Status: Active » Closed (duplicate)
Related issues: +#1253820: It's impossible to submit no value for a field that has a default value

Thank you for reporting this issue! It is a duplicate of another existing issue, #1253820: It's impossible to submit no value for a field that has a default value. This problem has been patched in 8.x, but the backport of the changes hasn't been worked on in a long time. Apparently there are some major problems to work out with it. If you would like to see the fix for this issue to move forward, please feel free to contribute to that issue.