Hi,

I have a content type that contains 3 fields:

  • Field A: a select list with two values V1, V2
  • Field B: a text field declared as required
  • Field C: a field collection

Field B is only visible if Field A value is V2 (dependency declared thanks to conditional fields).

If I try to submit a new content with Field A = V1 (then Field B is not visible), the form is submit with no error but Field C is not saved. It is logic because the field collection is saved only if there is no error in the form (code in field_collection_field_widget_embed_validate function). But, since the Field B is required, then an error is declared. This error is handled/cleared by conditional_fields_form_validate but it is called after all the other hooks (thanks to conditional_fields_module_implements_alter).

I don't know where we should fix the issue: in field collection module or in conditional fields...

What do you think ?

Comments

RyanPrice’s picture

I second the issue experienced here, my field collection values are not being saved and the functionality of my content type would mirror rfc2460's above.

dufferin’s picture

Same, here,

For ex : I have one group of fields, with conditional field. I'm using field collection as well.

When the dependee has value, the dependent (field collection) is visible.
On node/add or edit, If I add a value to the dependee, the dependent shows off. I fill up the fields. Save.

And what exactly happen is that the dependent fields keep their values, but the dependee don't, then values (dependent field collection) won't display on the node.
By the way the 'field collection' label and content won't display either.

But is it due to 'field group', 'conditional field' or 'field collection', or just the all don't play nice together...?

Update : because I'm on it right now, I see that removing a 'conditional field' make the values (previously dependent) shows up. So definitely a problem with 'conditional field' and 'field collection'.

Here is the error msg I get : Notice : Undefined index: und dans conditional_fields_evaluate_dependencies() (ligne 1020 dans /Applications/MAMP/htdocs/immobilier/sites/all/modules/conditional_fields/conditional_fields.module).

rfc2460’s picture

Status: Active » Closed (duplicate)

hi,

I now think that the issue is in the field collection module.

In function field_collection_field_widget_embed_validate of field_collection module (see code snippet below), it tests if there are errors in the form and not only the error coming from the validation of the field collection field. As we can have errors coming from required field hidden using conditional field, then the field collection is not saved. The trick is that conditional field module will manage (and clean) the errors relative to the dependents or the dependees but it is called at the end of the form validation process (in the function: conditional_fields_dependent_validate).

function field_collection_field_widget_embed_validate($element, &$form_state, $complete_form) {

  // .... 
  // Only if the form is being submitted, finish the collection entity and
  // prepare it for saving.
  if ($form_state['submitted'] && !form_get_errors()) {  // <---- HERE IS THE ISSUE
    //...
  }
}

So I rewrite this function in order to check only the errors coming from the field collection validation. I'm really sorry but I am not familiar with the patch process and I unfortunatly do not have time today to create a nice patch so here is the code I use now (and that solves my problem...)

function field_collection_field_widget_embed_validate($element, &$form_state, $complete_form) {
  $instance = field_widget_instance($element, $form_state);
  $field = field_widget_field($element, $form_state);
  $field_parents = $element['#field_parents'];
  $field_name = $element['#field_name'];
  $language = $element['#language'];

  $field_state = field_form_get_state($field_parents, $field_name, $language, $form_state);
  $field_collection_item = $field_state['entity'][$element['#delta']];

  // Attach field API validation of the embedded form.
  field_attach_form_validate('field_collection_item', $field_collection_item, $element, $form_state);

  // Variable to store the validation errors for the field collection
  // Errors are not directly set on the form to avoid compatibility issue with other modules
  // such as conditional_fields
  $field_collection_errors = array();
  // Now validate required elements if the entity is not empty.
  if (!field_collection_item_is_empty($field_collection_item) && !empty($element['#field_collection_required_elements'])) {
    foreach ($element['#field_collection_required_elements'] as &$elements) {

      // Copied from _form_validate().
      if (isset($elements['#needs_validation'])) {
        $is_empty_multiple = (!count($elements['#value']));
        $is_empty_string = (is_string($elements['#value']) && drupal_strlen(trim($elements['#value'])) == 0);
        $is_empty_value = ($elements['#value'] === 0);
        if ($is_empty_multiple || $is_empty_string || $is_empty_value) {
          if (isset($elements['#title'])) {
            $field_collection_errors[] = array('elements' => $elements,
              'message' => t('!name field is required.', array('!name' => $elements['#title'])),
            );
          }
          else {
            $field_collection_errors[] = array('elements' => $elements,
              'message' => '',
            );
          }
        }
      }
    }
  }

  // Only if the form is being submitted, finish the collection entity and
  // prepare it for saving.
  if ($form_state['submitted'] && sizeof($field_collection_errors) == 0) {

    field_attach_submit('field_collection_item', $field_collection_item, $element, $form_state);

    // Set the _weight if it is a multiple field.
    if (isset($element['_weight']) && ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED)) {
      $item['_weight'] = $element['_weight']['#value'];
    }

    // Put the field collection item in $item['entity'], so it is saved with
    // the host entity via hook_field_presave() / field API if it is not empty.
    // @see field_collection_field_presave()
    $item['entity'] = $field_collection_item;
    form_set_value($element, $item, $form_state);
  }

  // Add the errors to the form
  foreach ($field_collection_errors as $field_collection_error) {
    form_error($field_collection_error['elements'], $field_collection_error['message']);
  }
}

I will suggest a patch in the field condition module as soon as I have time (hopefully before the end of the week).

I close the ticket since it is not due to conditional field. Please follow #1549364: Data not being saved on initial creation for the patch.

@dufferin I am not sure your problem is identical to my issue, I suggest you create a new ticket for that.