1

I created a custom setpassword template which I largely copied from the default one except that I added firstName, lastName and photo fields so that the user can set up their whole account at the same time (the reason for their name not being entered earlier is that the users are created/invited by other users who may not have their name). But it seems that my added fields are being ignored in the submission. I assume it has something to do with the code that is being submitted in the form (and is in the query string) being very specific to one purpose here.

So any tips on the best way to do what I'm after, or am I just going to have to make it a 2 screen operation?

1 Answer 1

1

Without looking at the core code, I imagine that the setpassword action is only looking for the very specific fields it needs to reset the users password, saving the record, and redirecting to the next screen.

You could try creating a custom module that includes a hook to listen for the craft\elements\User::EVENT_BEFORE_SAVE event trigger.

Within that hook you could check the payload of the POST submission to see if it contains data from the extra fields you included, and if it does, manually set them in the user record that's about to be saved.

Maybe something roughly like this:

use yii\base\Event;
use craft\elements\User;
use craft\events\ModelEvent;

Event::on(
    User::class,
    User::EVENT_BEFORE_SAVE,
    function (ModelEvent $event) {
        $user = $event->element;

        if( !empty( craft.app.request.getBodyParam('YOUR_CUSTOM_FIELD') ) ) {
            $user->setFieldValue(
                'FIELD_NAME',
                craft.app.request.getBodyParam('YOUR_CUSTOM_FIELD')
            );
        }

        // repeat for other fields you're saving
        // ... etc
    }
);

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.