You are here

function flexiform_element_group_manage_form_fields_form_validate in Flexiform 7

Validation callback for flexiform_element_groups.

1 string reference to 'flexiform_element_group_manage_form_fields_form_validate'
flexiform_element_group_manage_form_fields_form_alter in flexiform_element_group/flexiform_element_group.admin.inc
Function to alter the form fields overview screen.

File

flexiform_element_group/flexiform_element_group.admin.inc, line 294
Contains alterations to the flexiform admin pages for element groups.

Code

function flexiform_element_group_manage_form_fields_form_validate($form, &$form_state) {
  $form_values = $form_state['values']['fields'];

  // This is a bit of a dirty hack but for the sake of working with field
  // group module we pretend the form name is the bundle of the flexiform
  // entity.
  $entity_type = 'flexiform';
  $bundle = $form['#flexiform']->form;
  $mode = 'form';
  $group = $form_values['_add_new_group'];

  // Validate if any information was provided in the 'add new group' row.
  if (array_filter(array(
    $group['label'],
    $group['group_name'],
  ))) {

    // Missing label.
    if (!$group['label']) {
      form_set_error('fields][_add_new_group][label', t('Add new group: you need to provide a label.'));
    }

    // Missing group name.
    if (!$group['group_name']) {
      form_set_error('fields][_add_new_group][group_name', t('Add new group: you need to provide a group name.'));
    }
    else {
      $group_name = $group['group_name'];

      // Add the 'group_' prefix.
      if (drupal_substr($group_name, 0, 6) != 'group_') {
        $group_name = 'group_' . $group_name;
        form_set_value($form['fields']['_add_new_group']['group_name'], $group_name, $form_state);
      }

      // Invalid group name.
      if (!preg_match('!^group_[a-z0-9_]+$!', $group_name)) {
        form_set_error('fields][_add_new_group][group_name', t('Add new group: the group name %group_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array(
          '%group_name' => $group_name,
        )));
      }
      if (drupal_strlen($group_name) > 32) {
        form_set_error('fields][_add_new_group][group_name', t("Add new group: the group name %group_name is too long. The name is limited to 32 characters, including the 'group_' prefix.", array(
          '%group_name' => $group_name,
        )));
      }

      // Group name already exists.
      if (field_group_exists($group_name, $entity_type, $bundle, $mode)) {
        form_set_error('fields][_add_new_group][group_name', t('Add new group: the group name %group_name already exists.', array(
          '%group_name' => $group_name,
        )));
      }
    }
  }
}