You are here

function user_badges_roles_form_validate in User Badges 7.3

Same name and namespace in other branches
  1. 6.2 user_badges.admin.inc \user_badges_roles_form_validate()
  2. 6 user_badges.admin.inc \user_badges_roles_form_validate()
  3. 7.4 includes/user_badges.admin.inc \user_badges_roles_form_validate()
  4. 7 user_badges.admin.inc \user_badges_roles_form_validate()
  5. 7.2 user_badges.admin.inc \user_badges_roles_form_validate()

Validation function for user_badges_roles_form.

File

./user_badges.admin.inc, line 733
@brief User Badges admin functions

Code

function user_badges_roles_form_validate($form, &$form_state) {

  // Get the user selected values from the multi-select/autocomplete.
  $array = $form_state['values']['roles'] + $form_state['values']['blocked'];

  // Initialize the $bids array which will store all the bids.
  $bids = array();

  // Go through all the entries and make sure they all have a valid badge ID.
  foreach ($array as $field => $value) {

    //If the selector type used to add badges was the autocomplete, then, validate the text in the autocomplete boxes
    if (variable_get('user_badges_selector_type', 1) == 1) {
      if (!empty($value)) {

        // The field isn't empty, so we should validate it.
        $validation = user_badges_badge_autocomplete_validation($value);

        //Is it correctly formatted?
        if ($validation[1] == 'string') {
          if ($field == 0) {
            form_set_error('blocked][' . $field, t('"@value" is not a valid badge name. Try using the autocomplete function (requires javascript).', array(
              '@value' => $value,
            )));
          }
          else {
            form_set_error('roles][' . $field, t('"@value" is not a valid badge name. Try using the autocomplete function (requires javascript).', array(
              '@value' => $value,
            )));
          }
        }
        elseif ($validation[1] == 'nobid') {
          if ($field == 0) {
            form_set_error('blocked][' . $field, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array(
              '@value' => $validation[0],
            )));
          }
          else {
            form_set_error('roles][' . $field, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array(
              '@value' => $validation[0],
            )));
          }
        }
      }
    }

    // Now we add the valid bids to an array so that we can check if there
    // are any duplicate bids assigned to a role.
    // Only add to the array if the selected value is not 'none' (multi-select)
    if ($value != NULL) {
      $bids[$field] = $value;
    }
  }

  // Now we check if there are duplicate bids in the $bids array and output
  // an error message if there are.
  if (isset($bids)) {
    $duplicate_bids = array_unique(array_diff_assoc($bids, array_unique($bids)));
    if (isset($duplicate_bids)) {
      foreach ($duplicate_bids as $field => $bid) {
        form_set_error('roles][' . $field, t('@bid has already been assigned to a role. Please choose another bid to assign to this role.', array(
          '@bid' => $bid,
        )));
      }
    }
  }
}