Hello,

i implemented fook_form_profile2_form_alter to alter the registration form. I succeeded in adding some custom element validate functions i needed, but i can't find a way to add ajax to my fields. here's the code i used:

function mymodule_form_profile2_form_alter(&$form, &$form_state) {
  // Your alterations.
  /*
   * - setting user infos about card number as username
   * - hiding profile card field and giving it a function to get the username value
   */
  if((arg(0)=="card")&&(arg(1)=="register")){
    
    $form['account']['name']['#type'] = "hidden";
    $form['account']['name']['#required'] = false;
    
    $form['profile_my_profile']["field_card_card"][$form['profile_my_profile']["field_card_card"]['#language']][0]['value']['#ajax'] = array(
      'callback' => 'my_module_new_user_ajax',
      'wrapper' => 'card_div',
      'method' => 'after'
    );
    $form['profile_my_profile']["field_card_card"][$form['profile_my_profile']["field_card_card"]['#language']][0]['value']['#prefix'] = '<div id="card_div">';
    $form['profile_my_profile']["field_card_card"][$form['profile_my_profile']["field_card_card"]['#language']][0]['value']['#suffix'] = '</div>';
    $form['account']['name']['#element_validate'][]="_my_profile_copy_card_to_username";
    
    if(isset($form_state['values']['profile_my_profile']["field_card_card"][$form['profile_my_profile']["field_card_card"]['#language']][0]['value'])){
      $card_n = $form['profile_my_profile']["field_card_card"][$form['profile_my_profile']["field_card_card"]['#language']][0]['value']['#value'];
      $result = _my_module_check_card_existing($card_n);
      if($result=="KO"){
          form_set_error("profile_my_profile][field_card_card",t("Sorry, the card number you inserted is not correct."));
      }
      
    }
    
    
    $form['#validate'][] = "_my_module_new_user";
     
  }

ajax is actually added to the field, but then ajax.inc fires:

Notice: Undefined index: #ajax in ajax_form_callback() (line 380 of /home/myuser/www/myproject/includes/ajax.inc).

so i went to check there and actually the file looks for $form_state['triggering_element']['#ajax']['callback'], but that element's #ajax is not set. is it because my form alter function is not called anymore?

thanks for helping

Comments

saru1683’s picture

I am also phase same problem.
I am using profile2 7.x-1.1 and latest D7.21.

I have tried to alter user register form with the help of form api, I have implement on hook_form_alter, hook_form_FORM_ID_alter and hook_form_profile2_form_alter but not working any of these.

I want to alter register form field please help me.

Thank You.

saru1683’s picture

If you want to alter profile2 field on user register form, you can do with the help of profile2 API, and it is hook_form_profile2_edit_PROFILE_TYPE_form_alter().

Thank You