Jump to content

Form Builder : role field to register users


Victor444
 Share

Recommended Posts

Hi everybody!

I have a problem trying to register users using form bulder. The field that I use is type PAGE and i can't specify a default role in form and let the field hidden.

How can specify the role of users registering?

Sorry but my english is very poor...

Attach some images to understande better.

Thanks!!

post-2254-0-55023800-1398264845_thumb.jp

post-2254-0-08505000-1398264847_thumb.jp

Link to comment
Share on other sites

  • 3 years later...

This works for me. User name is generated by PW from first name and last name fields. New user enters email, password and a couple of custom fields.

The user must be saved before the roles are added.

Hook developed with help, esp from Ryan, in the FormBuilder forum.

// Create new user from FormBuilder feu-register form
$forms->addHookAfter('InputfieldForm::processInput', null, 'hookCreateUser'); 

function hookCreateUser(HookEvent $event) {
    $form = $event->object;
    if($form->name != 'my-fb-form-name') return; // if it's not the form you want, return        
        
     // format the first_name and last_name as the username
    $sanitizer = wire('sanitizer');     

    $users = wire('users');
    
    // ensure email address is valid and unique    
    $email = $form->get('email'); 
    $emailValue = $sanitizer->selectorValue($email->value);
        
    if($emailValue) {           
        // check there aren't any other users with the same email
        $insiders = $users->find("include=all, email=$emailValue"); 
        if($insiders->count > 0) {
            $email->error(__("Sorry that email is already in use"));
            return false; 
        } 
    }
    
    // makes sure the username is not already in use.
    $first_name = $sanitizer->pageName($form->get('first_name')->value);
    $last_name  = $sanitizer->pageName($form->get('last_name')->value);
    if ($first_name && $last_name ) {
        $username = $first_name . '_' . $last_name;
        $u = $users->find("include=all, name^=$username");
        if ($u->count > 0) {
            $number = $u->count+1;
            $username = $username . $number;
        }
    } else {
        // invalid or blank
        $username->error(__("Please enter a valid username")); 
    }

        // ok, all checked, create user account
        
        $newUser = $users->add($username);    
        $newUser->of(false);
        $newUser->email = $emailValue;
        $newUser->pass = $form->get('password')->value;
        $newUser->first_name = $form->get('first_name')->value;
        $newUser->last_name = $form->get('last_name')->value;
        $newUser->country = $sanitizer->text($form->get('country')->value);
        $newUser->phone = $sanitizer->text($form->get('phone')->value);
        $newUser->save();
        $newUser->addRole('guest');
        $newUser->addRole('my-custom-user-role');    // change to your user role name    
        $newUser->save();
        $newUser->of(true);
}

 

  • Like 1
Link to comment
Share on other sites

Quote
37 minutes ago, Vayu Robins said:

Thank you very much! You wouldn't have the link to that forum posting where Ryan helped on this?

 

There are many. I trawled the FB forum on the assumption my requirement wasn't unique :)

Here's one:

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...