You are here

function fb_form_friend_selector_validate in Drupal for Facebook 6.3

Same name and namespace in other branches
  1. 5.2 fb_form.module \fb_form_friend_selector_validate()
  2. 6.2 fb_form.module \fb_form_friend_selector_validate()
  3. 7.3 fb_form.module \fb_form_friend_selector_validate()

File

./fb_form.module, line 337
This module defines facebook-specific form elements for use with Drupal's form API.

Code

function fb_form_friend_selector_validate($element, $set_errors = TRUE) {

  //dpm(func_get_args(), "fb_form_friend_selector_validate");
  if (!trim($element['#value'])) {
    return;
  }
  $names = explode(',', $element['#value']);
  $items = array();

  // Facebook user ids
  $result = $_SESSION['fb_form_friend_selector_result'];
  foreach ($names as $name) {
    $found = FALSE;
    foreach ($result as $data) {
      $fb_name = strtolower($data['name']);
      if (strtolower(trim($name)) == $fb_name) {
        if ($found) {

          // TODO: handle name collisions more gracefully!
          if ($set_errors) {
            form_set_error(implode('][', $element['#parents']), t('\'%name\' matched more than one friend.', array(
              '%name' => $name,
            )));
          }
          $found = -1;
        }
        else {
          $found = $data['uid'];
        }
      }
    }
    if ($found > 0) {

      // Running list of ids
      $items[] = $found;
    }
    elseif ($found == 0) {
      if ($set_errors) {
        form_set_error(implode('][', $element['#parents']), t('Could not find a friend named \'%name\'.', array(
          '%name' => $name,
        )));
      }
    }
  }

  // Make the submitted value a list of ids, not a comma-seperated list of names.
  form_set_value($element, $items);
  _form_set_value($_POST, $element, $element['#parents'], $items);
}