Bascially, we create an Ajax callback on the form element used to update the form, and we create an additional form element which is simply a container to display the the relevant profile form.
I used the Profile 2 Registration Path module to add the Profiles Forms to the User Registration form. I then use hide();
and show();
to display them if appropriate.
The Devel module was also invaluable doing this.
function rego_form_alter(&$form, &$form_state, $form_id){
if($form_id == 'user_register_form'){
dpm($form);
//First, hide all the other profiles
hide($form['profile_designer']);
//User selects what type of profile is most appropriate
$form['profile_main']['field_profile_type']['und']['#ajax'] = array(
'callback' => 'rego_update_profile_type',
'wrapper' => 'profiles_container',
);
//Create a container to the relevant profile
$form['profiles_container'] = array(
'#prefix' => '<div id="profiles_container">',
'#suffix' => '</div>',
'#weight' => '500',
);
}
}
//Callback
function rego_update_profile_type($form, $form_state){
// Switch based on the user input
switch($form_state['values']['profile_main']...['tid']){
case '82':
$form['profiles_container'] = $form['profile_designer'];
break;
// --Other cases here --
}
//Put back the div id so that the container can be updated again
$form['profiles_container']['#prefix'] = '<div id="profiles_container">';
$form['profiles_container']['#suffix'] = '</div>';
show($form['profiles_container']);
return $form['profiles_container'];
}