You are here

function conditional_fields_field_overview_form_validate in Conditional Fields 6.2

Same name and namespace in other branches
  1. 6 conditional_fields.module \conditional_fields_field_overview_form_validate()

Conditional fields in some situations can't change group.

1 string reference to 'conditional_fields_field_overview_form_validate'
conditional_fields_field_overview_form in ./conditional_fields.module
Find conditional fields and mark them.

File

./conditional_fields.module, line 1123
Content fields and groups visibility based on the values of user defined 'trigger' fields.

Code

function conditional_fields_field_overview_form_validate($form, $form_state) {
  $data = conditional_fields_load_data($form['#type_name']);

  // Build a list of controlled fields/groups.
  $controlled_fields = array();
  foreach ($data as $dependency) {
    $controlled_fields[$dependency['field_name']] = $dependency['field_name'];
  }
  foreach ($data as $dependency) {

    // If a field is inside a controlled group, it can control or be controlled only by fields in the same group,
    // so moving fields in disallowed positions is forbidden.
    $controlling_group = $form_state['values'][$dependency['control_field_name']]['parent'];
    $controlled_group = $form_state['values'][$dependency['field_name']]['parent'];

    // If the fields share the same parent (no group or same group), allow moving.
    if ($controlling_group == $controlled_group) {
      continue;
    }

    // If the fields have different parents, only allow parents that are not controlled.
    if (in_array($controlling_group, $controlled_fields) || in_array($controlled_group, $controlled_fields)) {
      form_set_error('', t('Fields inside a controlled group can only be controlling or be controlled by a field in the same group.'));
    }
  }
}