1

I am using Drupal 7 to create an registration form, there are: username, email dan password fields, but I want to make an username checking while user leave username field (ajax). I found an module as Username Check

This module is great and working in "myweb/user/register" form, but I need to move this function to my custom form in username field too

The code:

function username_check_menu() {
  $items = array();

  $items['username_check/isunique'] = array(
    'type' => MENU_CALLBACK,  
    'page callback' => 'username_check_callback',
    'access arguments' => array('access content'),
    'file' => 'username_check.unique.inc',
  );

  $items['admin/config/system/username_check'] = array(
    'title' => 'Username check',
    'type' => MENU_NORMAL_ITEM,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('username_check_settings'),
    'access arguments' => array('administer site configuration'),
    'file' => 'username_check.admin.inc',
  );

  return $items;
}

function username_check_form_user_register_form_alter(&$form) {

  $mode     = variable_get('username_check_mode', 'auto');

  if($mode != 'off') 
  {
    _username_check_load_resources($mode);
    $form['account']['name']['#field_suffix'] = '<span id="username-check-informer">&nbsp;</span>';
    $form['account']['name']['#suffix'] = '<div id="username-check-message"></div>';
  }

}

/**
 * Load username_check resources.
 */
function _username_check_load_resources($mode) {
  $module_path = drupal_get_path('module', 'username_check');
  drupal_add_css($module_path . '/username_check.css');
  drupal_add_js($module_path . '/username_check.js');

  drupal_add_js(array(
    'usernameCheck' => array(
      'ajaxUrl' => url('username_check/isunique', array('absolute' => TRUE)),
      'delay'   => variable_get('username_check_delay', 1),
    ),
  ), 'setting');
}

and my form as userregis_form How to apply this to my custom form?

1 Answer 1

0

you need to add a validation to your form via

    $form['#validate'][] = 'yourFunction';


  function yourFunction($form, &$form_state) {
        //to show the form values
        dpm($form_state);
        //do validation
  }
2
  • im successfull to load the css file, but the ajax file not working. @user3278675 can you specify ur mean as #validate form?
    – aanimation
    Jan 28, 2015 at 6:01
  • im not talking about the module. Im talking about making your own validation for the username. Is there any configuration needed for it to work or did you just enabled it and it work on other form? Jan 28, 2015 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.