First, Id like to state I had no idea things had changed. My site was still running xbox_gamertag 6.x-1.1
I'm assuming the project name change caused this but others may be out there still running xbox_gamertag like me. :)

Regarding the latest release of Gamertags, I would like to achieve validation.
I run custom addons to fields and if they are not properly validated, they return inadequate results.
For example, if a field is invalid, the data returned from the module will not work properly.
Xbox Live gamertags are 15 characters in length, including numbers, letters, and spaces
Playstation IDs are 16 characters in length, including numbers, letters, hyphens and underscores
Wii Friend Codes are 12 digit numbers (I believe.. may wish to check this one)
Steam ID uses numbers, letters and underscores but I do not know character length.

If the fields are validated to the corresponding service naming convention, adding to the module would be much easier.
Below is module code I used to validate a profile field to determine its length.Maybe it can assist someone.

<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  // check user_register or user_profile_form
  if ($form_id == 'user_register' || $form_id == 'user_profile_form') {
    // add Validation Function
    $form['#validate'][] = '_MYMODULE_form_alter_user_profile_validate';
  }
}
// Validation Function
function _MYMODULE_form_alter_user_profile_validate($form, &$form_state) {
  // Validate the gamertag (number,letter,spaces)(15 character length) and return form error if invalid
  $user_gamertag = $form_state['values']['profile_xbl_gamertag'];
  if (!preg_match('!^[a-zA-Z0-9 ]+$!', $user_gamertag))
	form_set_error('profile_xbl_gamertag', 'XBL Gamertags only contain numbers, letters and spaces.');
  if (strlen($user_gamertag) >= 16)
    form_set_error('profile_xbl_gamertag', 'XBL Gamertags cannot exceed 15 characters.');
}
?>

Another good reason to validate the user input is that although Drupal filters the input, someone could still insert

or other attempts at malicious code. This will not affect the security of the module or site but if xbox.com keeps seeing continued requests for an iframe with http://gamercard.xbox.com/<script> , they may have security filters in place to block IP. I havent had a chance to look into your module as I just found it tonight but i'm hoping someone can integrate validation checks into it. If noone wants to take a shot at this, I'll attempt it as time allows. Thanks for the great module!

Comments

xDudditzx’s picture

oops, just realized I had stacked conditions.
I decided to apply it to module anyway and added to the gamertags module at my site like so:

/**
 * Validates Gamertags for user input forms
 */
function gamertags_form_alter(&$form, $form_state, $form_id) {
  // check user_register or user_profile_form
  if ($form_id == 'user_register' || $form_id == 'user_profile_form') {
    // add Validation Function
    $form['#validate'][] = '_gamertags_form_alter_gamertags_validate';
  }
}
// Validation Function
function _gamertags_form_alter_gamertags_validate($form, &$form_state) {
  // Validate the gamertags

  // PSN Gamertag
  $psn_gamertag = $form_state['values']['psn_gamertag'];
  if ( (!empty($psn_gamertag) && (!preg_match('!^[a-zA-Z0-9-_]+$!', $psn_gamertag)) || (strlen($psn_gamertag) >= 17) ) )
	form_set_error('psn_gamertag', 'Playstation: numbers letters hyphens and underscores up to 16 characters in length');

  // PSN Region
  //$psn_region = $form_state['values']['psn_region'];

  // Xbox Gamertag
  $xbox_gamertag = $form_state['values']['xbox_gamertag'];
  if ( (!empty($xbox_gamertag) && (!preg_match('!^[a-zA-Z0-9 ]+$!', $xbox_gamertag)) || (strlen($xbox_gamertag) >= 16) ) )
	form_set_error('xbox_gamertag', 'Xbox Gamertag: numbers letters and spaces up to 15 characters in length');

  // Wii Gamertag
  $wii_gamertag = $form_state['values']['wii_gamertag'];
  if ( (!empty($wii_gamertag) && (!preg_match('!^[0-9]+$!', $wii_gamertag)) || (strlen($wii_gamertag) != 12) ) )
	form_set_error('wii_gamertag', 'Wii Friend Codes are 12 digit numbers only');

  // Steam Gamertag
  //$steam_gamertag = $form_state['values']['steam_gamertag'];
}

The code still needs finished but it does work for user register and user edit.
I have not tested it against module permissions, etc and it could probably be written differently but theres a start.

pobster’s picture

Nice, I like it. Are you *absolutely* sure about all the specifics though?... (BTW Wii friendcodes are 16 digits four blocks of four digits)

If you're absolutely positive the other conditions are correct I'll commit them - so let me know!

As for the comment about updating the project name, well... Gamertags actually *is* xbox_gamertags - check out the releases http://drupal.org/node/76771/release I wasn't sure how Drupal would handle it and as you can see, it didn't handle it at all!

Pobster

pobster’s picture

Status: Active » Fixed

Added to dev branch for testing; http://drupalcode.org/viewvc/drupal/contributions/modules/gamertags/game...

Will make general release once you clarify the specs.

Pobster

xDudditzx’s picture

Im not completely positive about the settings - (thanks for the Wii notice).
And the php code could probably be better as Im fairly new to it.
I did go to the Xbox and Playstation sites to register an ID and received the following:
Xbox Gamertag
Gamertags can only contain letters, numbers, and spaces, and can't begin with a number.
I know the maximum gamertag length is 15 characters from being an avid Xbox gamer.
And Wiki states the following:
A Gamertag used online must be unique and can be up to 15 characters in length, including numbers, letters, and spaces.
http://en.wikipedia.org/wiki/Xbox_Live#Gamertag

Playstation Network
The online ID must:
- contain from 3 to 16 characters
- start with an alphabet letter
- not match the sign-in ID or password
The data below pulled from Wiki http://en.wikipedia.org/wiki/Playstation_Network#Online_ID
An online ID used online must be unique and can be up to 16 characters in length, including numbers, letters, hyphens and underscores.

I dont have a Wii but I was mistaken about friend code - thank you for correction.

pobster’s picture

Hiya! There's nothing wrong with the code, I've mostly kept it as it was although as you've probably noticed - there's no need for the form_alter as hook_user takes care of providing the validation callback.

Thanks, I really appreciate the info and idea! I'll rewrite the regular expression to take account of the "can't begin with numbers" clause.

Pobster

pobster’s picture

Hiya, apologies for the delay - are you absolutely sure about the Xbox + PS3 gamertags not being able to start with numbers? I can't find any reference to it on the Wikipedia pages?

Pobster

xDudditzx’s picture

Thats what I received when attempting to register a new ID with PSN and Xbox at their websites.

pobster’s picture

Awesome, thanks - well I'd committed it anyway ;o) I'll make a new release a bit later today. Thanks for all your help with this.

Pobster

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.