You are here

function redhen_fields_allowed_labels_setting_validate in RedHen CRM 7

Element validate callback; check that the entered values are valid.

1 string reference to 'redhen_fields_allowed_labels_setting_validate'
redhen_fields_field_settings_form in modules/redhen_fields/redhen_fields.module
Implements hook_field_settings_form().

File

modules/redhen_fields/redhen_fields.module, line 149
Defines email, phone and address field types for RedHen CRM.

Code

function redhen_fields_allowed_labels_setting_validate($element, &$form_state) {
  $field = $element['#field'];
  $has_data = $element['#field_has_data'];
  $generate_keys = !$has_data;
  $values = redhen_fields_extract_allowed_labels($element['#value'], $generate_keys);
  if (!is_array($values)) {
    form_error($element, t('Allowed labels list: invalid input.'));
  }
  else {

    // Check that keys are valid for the field type.
    foreach ($values as $key => $value) {
      if (!preg_match('/^-?\\d+$/', $key) || $key === 0) {
        form_error($element, t('Allowed labels list: keys must be positive integers greater than zero.'));
        break;
      }
    }

    // Prevent removing values currently in use.
    if ($has_data) {
      $lost_keys = array_diff(array_keys($field['settings']['labels']), array_keys($values));
      if (redhen_fields_labels_in_use($field, $lost_keys)) {
        form_error($element, t('Allowed labels list: some labels are being removed while currently in use.'));
      }
    }
    form_set_value($element, $values, $form_state);
  }
}