You are here

function notifications_custom_fields_form_validate in Notifications 6

Same name and namespace in other branches
  1. 6.4 notifications_custom/notifications_custom.admin.inc \notifications_custom_fields_form_validate()

Validate field values

File

notifications_custom/notifications_custom.admin.inc, line 247

Code

function notifications_custom_fields_form_validate($form, &$form_state) {
  $field_values = array();
  $fields = $form_state['values']['fields'];
  if (!empty($fields['type'])) {
    foreach ($fields['type'] as $fid => $type) {
      $value = NULL;

      // Remove if checked for deletion
      if (!empty($fields['delete'][$fid])) {
        unset($form_state['values']['fields']['type'][$fid]);
        continue;
      }
      elseif (!empty($fields['value'][$fid])) {
        $value = $fields['value'][$fid];

        // We may need additional validation or field - value mappging for some fields
        if ($callback = notifications_subscription_fields($type, 'value callback')) {
          $value = call_user_func($callback, $value, "fields][value][{$fid}");
        }

        // If we still have a value (mapping may have failed, go and save)
        if ($value) {
          $field_values[] = array(
            'type' => $type,
            'value' => $value,
          );
        }
      }
      if (!$value) {

        // We don't have a value, error message
        form_set_error("fields][value][{$fid}", t('You must set a value for this field.'));
      }
    }

    // Final check, we should have some valid field/value pairs
    if ($field_values) {
      $form_state['field_values'] = $field_values;
    }
    else {
      form_set_error(NULL, t('You must set at least one field for this subscription type.'));
    }
  }
}