You are here

function notifications_add_subscription_form_validate in Notifications 6.3

Same name and namespace in other branches
  1. 6 notifications.pages.inc \notifications_add_subscription_form_validate()
  2. 6.2 notifications.pages.inc \notifications_add_subscription_form_validate()

Validate new subscription, compute actual field values

File

./notifications.pages.inc, line 120
User pages for Notifications

Code

function notifications_add_subscription_form_validate($form, &$form_state) {
  $field_values = array();
  foreach ($form_state['values']['fields'] as $fid => $field) {

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

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

      // We don't have a value, error message
      form_set_error("fields][{$fid}][value", 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('', t('You must provide valid values for all fields.'));
  }
}