<p>Could you try this at the end of the function::</p>
<pre><code>    $user-&gt;setFieldValue($fieldName, 'I want this text to populate exampleTextField')
    // ... more code
    Craft::$app-&gt;getElements()-&gt;saveElement($user);
</code></pre>
<p>Also, I think it's better to use AFTER SAVE rather than BEFORE(It really depends on how your module works)</p>
<p><strong>UPD</strong>:
Try these variants and also see this <a href="https://craftcms.stackexchange.com/questions/26772/how-to-saveelement-in-event-after-save-element-without-infinite-loop">question</a>, maybe it will help you</p>
<p>#1</p>
<pre><code>$fields = $user-&gt;getFieldValues();

$fields['customHandle'] = 'Custom value';
                    
$user-&gt;setFieldValues($fields);
</code></pre>
<p>#2</p>
<pre><code>$fields = $user-&gt;getSerializedFieldValues();

$fields['customHandle'] = serialize('Custom value');
                    
$user-&gt;setFieldValues($fields);
</code></pre>
<p>#3</p>
<pre><code>$user-&gt;customHandle = 'Custom value';
</code></pre>
<p>P.S. By the way, try wrapping the value in an array, maybe that will help you too.</p>
<pre><code> $user-&gt;setFieldValue('userAvatar', [$avatarAssetId]);
</code></pre>
<p><strong>UPD2</strong>: You have an error in logic. You are trying to save a user (element) that is not a sender. You won't be able to save it in BEFORE/AFTER element save event, as any attempt to save it via:</p>
<pre><code>Craft::$app-&gt;getElements()-&gt;saveElement($user);
</code></pre>
<p>will result in an endless loop.
You need to send data to <a href="https://craftcms.com/docs/3.x/extend/controllers.html" rel="nofollow noreferrer">a custom controller</a> and handle the data there as you need. By the way, you will need to check permissions of users manually, if it's necessary.</p>
<pre><code>if ($user-&gt;can('editUsers')) {
....
}
</code></pre>

CC BY-SA 4.0
2022-07-11T13:24:08.450Z
I've attempted to use `Craft::$app->getElements()->saveElement($user);` in the past but each time I do, it send the module into an infinite loop. This occurs on both BEFORE SAVE and AFTER SAVE, unfortunately.
Mike Niland
CC BY-SA 4.0
2022-07-11T18:29:14.260Z
Oh yes, of course I was wrong, never do that, because after saving you try to save again, and then save again...
Check update of my answer again
RomanAvr
CC BY-SA 4.0
2022-07-11T22:06:15.530Z
Much appreciated but I attempted 1-3 using BEFORE and AFTER save, no luck. I can log the original stored value from the variable so it's accessing the correct variable and value assigned to the user but `$user->setFieldValues($fields);` or `$user->setFieldValue('fieldHandle', ['new copy']);` can't seem to overwrite the original value even with admin permissions .
Mike Niland
CC BY-SA 4.0
2022-07-12T04:54:34.470Z
I saw your mistake.
You are trying to find the user and write it down to a new variable altogether, but instead you have to use the user that passes in the variable from the event.
You also write in the comments that the user you are trying to save is another user, not the sender.
Therefore, as I think, the best solution would be to send your form to your custom controller, where you will process the user as you need.
Otherwise, you cannot save another user, as this will cause an infinite loop.
RomanAvr
CC BY-SA 4.0
2022-07-12T04:54:49.173Z
In theory, you can somehow see if this user has been saved recently and skip it, for example, by the update date, but this solution seems to me not very correct.
RomanAvr
CC BY-SA 4.0
2022-07-13T02:29:28.367Z
Thank you for the explanation. I thought I could simply mimic the hidden input behavior within the module on save (i was able to achieve the desired result with hidden inputs, just didn't want the field data to be visible on the front end). I'll use a custom controller instead.
Mike Niland
CC BY-SA 4.0
2022-07-09T06:40:25.813Z
2022-07-12T06:26:58.037Z
2022-07-12T06:26:58.037Z
RomanAvr
<p>I'm attempting to allow a user to edit other users via a front-end form. The &quot;Edit users&quot; permission has been checked in the admin but when I submit the form the data doesn't save. For this example, I'd prefer not to use hidden inputs within the form to manipulate the data.</p>
<pre><code>Event::on(
 User::class,
 User::EVENT_BEFORE_SAVE,
 function (ModelEvent $event) {

    //User id is the id of another user, not the sender.

    $userId = $this-&gt;request-&gt;getBodyParam('userId');
    $user = Craft::$app-&gt;users-&gt;getUserById($userId);

    $fieldName = 'exampleTextField';
    $user-&gt;setFieldValue($fieldName, 'I want this text to populate exampleTextField');

});
</code></pre>

<users><modules><craft4>
How do I allow a user to edit another users data via module?
# How do I allow a user to edit another users data via module?

(Asked by: username_0 on 2022-07-08)

I'm attempting to allow a user to edit other users via a front-end form. The "Edit users" permission has been checked in the admin but when I submit the form the data doesn't save. For this example, I'd prefer not to use hidden inputs within the form to manipulate the data.
Event::on(
 User::class,
 User::EVENT_BEFORE_SAVE,
 function (ModelEvent $event) {

    //User id is the id of another user, not the sender.

    $userId = $this->request->getBodyParam('userId');
    $user = Craft::$app->users->getUserById($userId);

    $fieldName = 'exampleTextField';
    $user->setFieldValue($fieldName, 'I want this text to populate exampleTextField');

});

**Tags:** users, modules, craft4


---

## 1 answers

### Answer by username_1 on 2022-07-09. Score: 1 | Accepted answer

Could you try this at the end of the function::
    $user->setFieldValue($fieldName, 'I want this text to populate exampleTextField')
    // ... more code
    Craft::$app->getElements()->saveElement($user);

Also, I think it's better to use AFTER SAVE rather than BEFORE(It really depends on how your module works)
UPD:
Try these variants and also see this question, maybe it will help you
#1
$fields = $user->getFieldValues();

$fields['customHandle'] = 'Custom value';
                    
$user->setFieldValues($fields);

#2
$fields = $user->getSerializedFieldValues();

$fields['customHandle'] = serialize('Custom value');
                    
$user->setFieldValues($fields);

#3
$user->customHandle = 'Custom value';

P.S. By the way, try wrapping the value in an array, maybe that will help you too.
 $user->setFieldValue('userAvatar', [$avatarAssetId]);

UPD2: You have an error in logic. You are trying to save a user (element) that is not a sender. You won't be able to save it in BEFORE/AFTER element save event, as any attempt to save it via:
Craft::$app->getElements()->saveElement($user);

will result in an endless loop.
You need to send data to a custom controller and handle the data there as you need. By the way, you will need to check permissions of users manually, if it's necessary.
if ($user->can('editUsers')) {
....
}

> **Comments:**
> **username_0**: I've attempted to use `Craft::$app->getElements()->saveElement($user);` in the past but each time I do, it send the module into an infinite loop. This occurs on both BEFORE SAVE and AFTER SAVE, unfortunately.  
> **username_1**: Oh yes, of course I was wrong, never do that, because after saving you try to save again, and then save again... Check update of my answer again  
> **username_0**: Much appreciated but I attempted 1-3 using BEFORE and AFTER save, no luck. I can log the original stored value from the variable so it's accessing the correct variable and value assigned to the user but `$user->setFieldValues($fields);` or `$user->setFieldValue('fieldHandle', ['new copy']);` can't seem to overwrite the original value even with admin permissions .  
> **username_1**: I saw your mistake. You are trying to find the user and write it down to a new variable altogether, but instead you have to use the user that passes in the variable from the event. You also write in the comments that the user you are trying to save is another user, not the sender. Therefore, as I think, the best solution would be to send your form to your custom controller, where you will process the user as you need. Otherwise, you cannot save another user, as this will cause an infinite loop.  
> **username_1**: In theory, you can somehow see if this user has been saved recently and skip it, for example, by the update date, but this solution seems to me not very correct.  
> **username_0**: Thank you for the explanation. I thought I could simply mimic the hidden input behavior within the module on save (i was able to achieve the desired result with hidden inputs, just didn't want the field data to be visible on the front end). I'll use a custom controller instead.  

---

