0

So, I need to create a user registration form for a website that has numerous types of users that are distinctly different from one another. I've already found the profiles module, and I'm using that. But I want to streamline the user registration process.

We don't want to create a large form that pushes everyone through the same questions, even if that question isn't applicable to that user. Nor do we want to compromise and only not ask certain questions that are important to that particular type of user.

I'd also prefer the form to be dynamically updated, rather than a multi-step form.

How can I do this?

1

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'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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