1

I have 2 forms on my site, one that registers for the WHOLE site and one that registers for just the downloads on the site. I wrote this custom function to change the user group if the email existed. So basically what i'm going for here is if someone signed up for ONLY the downloads and wanted to signup for the whole site to let them use their email they used to sign up originally.

So basically they fill out the sign up form use the same email etc.

Currently the code i have isn't working properly. It changes the user group and allows the user to sign up IF they already signed up, however it won't update the rest of my user fields on the form. If a new user comes to the site and tries to sign up for the whole site the signup form doesn't work, it doesn't save the user, just redirects to the login page.

enter image description here

  • You need to save/create the user in both scenarios if you want to update fields. So to keep it simple (please leave all not necessary information) all your question is about "how to create/update a user"? If I remember correctly you asked this question already a few days ago – Robin Schambach Sep 25 '18 at 17:08
  • I asked a similar question 13 days ago, i hadn't started developing the functionality yet. How do i go about doing it in both scenarios ? – Kevin Love Sep 25 '18 at 17:35
1

To Create a user do

$request = Craft::$app->getRequest();
$user = new User();
$user->username         = $request->getBodyParam('username');
$user->email            = $request->getBodyParam('email');
$user->newPassword      = $request->getBodyParam('password');
$user->firstName        = $request->getBodyParam('firstName');
$user->lastName         = $request->getBodyParam('firstName');
$user->setScenario(User::SCENARIO_REGISTRATION);
Craft::$app->getElements()->saveElement($user);

To update a user

$request            = Craft::$app->getRequest();
$user->username     = $request->getBodyParam('username');
$user->email        = $request->getBodyParam('email');
$user->newPassword  = $request->getBodyParam('password');
$user->firstName    = $request->getBodyParam('firstName');
$user->lastName     = $request->getBodyParam('firstName');
$user->setScenario(User::SCENARIO_LIVE);
Craft::$app->getElements()->saveElement($user);

So it's basically the same except the scenario

  • What if the fields are custom? – Billy Sengsey Nov 9 '18 at 11:48
  • You mean how to populate fields in the field layout? – Robin Schambach Nov 9 '18 at 11:55
  • @BillySengsey or @ anyone who arrives here through Google... custom fields are as simple as using the field handle: $user->customField = 'whatever'; Matrix fields will be a different challenge however... – James Smith Oct 3 at 13:11

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.