For some reason, the "request" validation is not working on the user registration page.

I have the "request" validation working on the login block and I can get the "email" validation working on the registration page.

Here is my current code:

function msnet_menu() {
  $items = array();
  
  $items[] = array(
    'path' => 'validate/user/' . arg(2),
    'title' => t(''),
    'callback' => 'msnet_validate_user',
    'callback arguments' => array(arg(2)),
    'access' => TRUE,
  );
  
  $items[] = array(
    'path' => 'validate/register/user/' . arg(2),
    'title' => t(''),
    'callback' => 'msnet_validate_register_user',
    'callback arguments' => array(arg(2)),
    'access' => TRUE,
  );
  
  return $items;
}

function msnet_form_alter($form_id, &$form) {
  if ($form_id == 'user_login' || $form_id == 'user_login_block') {
    $form['name']['#attributes']['class'] = 'request';
    $form['name']['#attributes']['validator-path'] = 'validate/user';
  } elseif ($form_id == 'user_register') {
    $form['account']['name']['#attributes']['class'] = 'request';
    $form['account']['name']['#attributes']['validator-path'] = 'validate/register/user';
    $form['account']['mail']['#attributes']['class'] = 'email';
  }
}

function msnet_validate_user($username) {
  $output = array();
  $username = check_plain($username);
  
  if ($user = user_load(array('name' => $username))) {
    $output['valid'] = TRUE;
  } else {
    $output['valid'] = FALSE;
    $output['message'] = t('Sorry, the user <strong>!username</strong> was not found.', array('!username' => $username));
  }
  
  echo json_encode($output);
  exit;
}

function msnet_validate_register_user($username) {
  $output = array();
  $username = check_plain($username);

  if ($user = user_load(array('name' => $username))) {
    $output['valid'] = FALSE;
    $output['message'] = t('Sorry, that username is already taken. Please try another usernmae.');
  } else {
    $output['valid'] = TRUE;
  }
  
  echo json_encode($output);
  exit;
}

So what am I missing?

Comments

tjholowaychuk’s picture

Hmm at a glance that looks perfect, are you receiving any kind of error ?

denney’s picture

No. No error, no nothing.

Nothing logged in watchdog or in Firefox's error console.

tjholowaychuk’s picture

hmm.
the request event handler is fired on 'change'

$('.request').change(function(){ validator.request(this); });

but you would still be seeing something.. and the email validation is working? its hard to tell without me testing out your code.

if you go directly to the url validate/register/user/someString do you see the JSON output?

is your drupal installed in a subdirectory?

my example was very similar to what you are doing there so its suprising that its not working it looks fine but hopefully we can find out whats going on !

tjholowaychuk’s picture

I just tried pretty much the same setup as you with the current release, everything is working atleast with my setup. you might want to try changing

'validate/register/user'

to

'/validate/register/user'

denney’s picture

My Drupal isn't in a subdirectory.

I tried direct access to the link and it returns a "Page Not Found" error!

I worked out that seems to have trouble with paths longer than 2 levels deep (eg '/something/somethingelse/thirdlevel').

It was also rather touchy with the '/' at the start of the 'validator-path'. Sometimes worked, sometimes didn't.

tjholowaychuk’s picture

The path problem there might have been a caching issue, the segments in the path shouldnt really matter there could be 10 if you wanted, but if it was showing page not found before then that was definatly just the hook_menu issue. As far as the validator-path working/not working with the / at the beginning of the path the only reason I could imagine that not working would be if drupal was in a sub directory, let me know if you figure anything out

tjholowaychuk’s picture

Status: Active » Closed (fixed)
denney’s picture

You're not going to believe this... If you take a look at the code I originally posted, you will notice that my path is 'validate/user/register' and I'm using 'arg(2)'.

In order for it to work properly, it should be 'arg(3)' as there is 4 components in the URL!!!!!

Teaches me for just copying and pasting!

denney’s picture

Also, the reason I had problems with the '/' is, I think, because I'm calling it from a sub path... '/user/register' which would mean it was trying to access '/user/register/validate/register/user'.

denney’s picture

Status: Closed (fixed) » Fixed
tjholowaychuk’s picture

haha funny how that works hey! I try to assume its the dumb things first but it definatly slips by alot

Anonymous’s picture

Status: Fixed » Closed (fixed)