Rich Torres Posted August 20, 2018 Share Posted August 20, 2018 I am a bit confused. I have a simple form to add new users, and I damn near have everything working. As a matter of fact it WAS working just fine a few times while testing. I really changed very little. The role "representative" exists yet I still get an error: Quote Error: Exception: Method NullPage::addRole does not exist or is not callable in this context <form method="post" action="./"> <label for="firstName">First Name</label> <input name="firstName" type="text" required> <label for="lastName">Last Name</label> <input name="lastName" type="text" required> <label for="email">Email</label> <input name="email" type="email" required> <button name="submit" type="submit">Get Started</button> </form> <?php if (isset($_POST['submit'])) { // Puts users in an array, and finds a userID that is not being used $agentList = array(); $number = 1001; foreach($users->find('start=0, roles=representative') as $v) { array_push($agentList, $v->userID); } for ($number; ;) { if(in_array($number, $agentList)) { $number++; } else { $newAgentID = $number; break; } } // Place POST data into variables $firstName = $_POST['firstName']; $lastName = $_POST['lastName']; $fullName = array($firstName,$lastName); $email = $_POST['email']; //Have Processwire add the new user $u = $users->add($firstName[0].$lastName); $u->firstName = $firstName; $u->lastName = $lastName; $u->pass = "Password"; $u->addRole('representative'); $u->email = $email; $u->userID = $newAgentID; $u->save(); //Add user to ViciDial $viciAdminUser = 10; $viciAdminPass = "password"; $viciUser = $newAgentID; $viciUserName = implode(" ",$fullName); $viciUserPass = "Password".$newAgentID; $viciUserLevel = 1; $viciUserGroup = "Arizona"; } Link to comment Share on other sites More sharing options...
BitPoet Posted August 21, 2018 Share Posted August 21, 2018 The message complains about $u being empty (i.e. a NullPage object), so the call to $users->add didn't succeed. A likely cause is that a user with the name you are trying to add already exists. You should check the return of $users->add, or even better, check if the name in question is already there beforehand and output a message accordingly (and don't forget to sanitize the form input). There's also a nice API to access GET and POST parameters in ProcessWire. Instead of using e.g. $_POST["submit"], you can access $input->post->submit and don't trigger warnings/errors when that parameter isn't defined. 6 Link to comment Share on other sites More sharing options...
dragan Posted August 21, 2018 Share Posted August 21, 2018 Just a guess: Maybe you need to save the new user first, and then add the role. Also, this looks strange: for ($number; ;) { Link to comment Share on other sites More sharing options...
Rich Torres Posted August 21, 2018 Author Share Posted August 21, 2018 @BitPoet You were dead on! User existed. That was impressive. No wonder is worked some times, but not others. Also, thank you for the API insight. @dragan The purpose of the for is to loop through existing user ID #s to find one that does not exist. I am not sure if my method was the best, but it works. 1 Link to comment Share on other sites More sharing options...
Noel Boss Posted August 22, 2018 Share Posted August 22, 2018 @Rich Torres You can create a new user like this: $u = new Page($templates->get('user')); Link to comment Share on other sites More sharing options...
adrian Posted August 23, 2018 Share Posted August 23, 2018 On 8/22/2018 at 4:54 AM, Noel Boss said: @Rich Torres You can create a new user like this: $u = new Page($templates->get('user')); This is much easier ? $u = $users->add('user-name'); 4 Link to comment Share on other sites More sharing options...
Dennis Spohr Posted May 5, 2020 Share Posted May 5, 2020 I have to reopen this thread, because I still have these problems. I am absolutely sure that my user-name is valid and unique. Still, if using $users->add($name) I'm getting a NullPage back, but just sometimes. I couldn't find a way to reproduce this error on our test-server. Our database is huge, we have some traffic and probably sometimes users are created nearly simultaneously (but definitely with unique names) Currently we have more than 150k user-accounts in the system. Do you think that could be a problem? The solution of @Noel Boss is interesting. How will the user-name be generated in this case? Any help is really appreciated! Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now