You are here

function civicrm_entity_form_validate in CiviCRM Entity 7.2

Same name and namespace in other branches
  1. 7 civicrm_entity.module \civicrm_entity_form_validate()

Form API validate callback for the entity form

1 string reference to 'civicrm_entity_form_validate'
civicrm_entity_form in ./civicrm_entity.default_form.inc

File

./civicrm_entity.default_form.inc, line 323

Code

function civicrm_entity_form_validate(&$form, &$form_state) {
  $entity = $form['#entity'];
  $entity_type = $form['#entity_type'];

  // unset form and entity values if civicrm properties are hidden from the form by display suite
  if (module_exists('ds_forms')) {
    if ($ds_form = ds_build_load($form, $form['#form_id'])) {
      if ($layout = ds_get_layout($ds_form->entity_type, $ds_form->bundle, 'form', FALSE)) {
        foreach ($layout['settings']['fields'] as $field_name => $visibility) {
          if ($field_name != 'id' && $visibility == 'hidden' && strpos($field_name, 'field_') !== 0) {
            unset($form_state['values'][$field_name]);
            unset($entity->{$field_name});
          }
        }
      }
    }
  }
  foreach ($form_state['values'] as $key => $value) {
    if (!is_array($value)) {
      $entity->{$key} = $value;
    }
    elseif (isset($value['value'])) {
      $entity->{$key} = $value['value'];
    }
    else {
      $entity->{$key} = $value;
    }
  }

  // validate civicrm data
  $entity_metadata_info = entity_get_property_info($entity_type);
  foreach ($entity_metadata_info['properties'] as $name => $info) {
    if (!empty($info['type'])) {

      // integer validation
      if ($info['type'] == 'integer') {

        //text field validation
        if (isset($form[$name]['#type']) && $form[$name]['#type'] == 'textfield') {
          if (isset($form_state['values'][$name]) && strlen($form_state['values'][$name]) && (string) (int) $form_state['values'][$name] !== (string) $form_state['values'][$name]) {
            form_set_error($name, t('Value must be an integer. e.g (1, 154, 0)'));
          }
        }
      }
      elseif ($info['type'] == 'decimal') {

        //text field validation
        if (isset($form[$name]['#type']) && $form[$name]['#type'] == 'textfield') {
          if (isset($form_state['values'][$name]) && strlen($form_state['values'][$name]) && !is_numeric($form_state['values'][$name])) {
            form_set_error($name, t('Value must be an number. e.g (1, 1.26, -1.3)'));
          }
        }
      }
    }
  }

  // validate name for contact
  if ($entity_type == 'civicrm_contact') {
    if ($form_state['values']['contact_type'] == 'Individual') {
      if (!isset($form['first_name']) && !isset($form['middle_name']) && !isset($form['last_name']) && !isset($form['display_name'])) {
        form_set_error('contact_type', t('Individual contact type, form must include at least one of first, middle, last or display name fields.'));
      }
      elseif (empty($form_state['values']['first_name']) && empty($form_state['values']['middle_name']) && empty($form_state['values']['last_name']) && empty($form_state['values']['display_name'])) {
        form_set_error('first_name', t('At least one of first, middle, last or display names must be set when contact type is set to Individual.'));
        form_set_error('middle_name');
        form_set_error('last_name');
        form_set_error('display_name');
      }
    }
    elseif ($form_state['values']['contact_type'] == 'Household') {
      if (!isset($form['household_name'])) {
        form_set_error('contact_type', t('Household contact type, form must include the household name field.'));
      }
      elseif (empty($form_state['values']['household_name'])) {
        form_set_error('household_name', t('Household name is required when contact type is set to Household'));
      }
    }
    elseif ($form_state['values']['contact_type'] == 'Organization') {
      if (!isset($form['organization_name'])) {
        form_set_error('contact_type', t('Organization contact type, form must include the organization name field.'));
      }
      elseif (empty($form_state['values']['organization_name'])) {
        form_set_error('organization_name', t('Organization name is required when contact type is set to Organization.'));
      }
    }
  }

  // Notify field widgets to validate their data.
  field_attach_form_validate($entity_type, $entity, $form, $form_state);
}