Update user from front end

I am trying to update the user content file from the front end, specifically appendending to a structure field.

After looking at some solutions on the forum, I put this together in a controller:

<?php

return function ($kirby) {
    $error = '';
    if ($kirby->request()->is('POST') && get('unit')) {

        try {
            $kirby->impersonate('kirby');
           
            if(get('unit') !== ''){ 


                $submission = $kirby->user()->homework()->yaml();

                $submission[] = [
                'unitname' => 'The name of the unit',
                'submission' => 'url',
                'grade' => 'false'
                ];

                $kirby->user()->update([
                'homework' => Yaml::encode($submission)
                ], 'en');

                $kirby->user()->update([
                'homework' => Yaml::encode($submission)
                ], 'kr');
              

            }
    
        } catch (Exception $e) {
          $error = error_message;
        }
    
      }
    
      return [
        'error' => $error
      ];

};

Unfourtunately this doesnt work and dont get any error messages. What am I missing. This user role does not have panel access but it still doesnt work if i grant access.

Nevermind, turned out user update was set to false.

Spoke too soon - it works directly in the template but not in the controller when triggered by the form.

Would appreciate some help there.

My form looks like this

<?php if ($user = $kirby->user()) : ?>
<form method="POST" action="<?= $page->url() ?>">

  <div class="form-input">
    <?= label("Submit Unit", 'unit', 'label-class') ?>
    <?= input('text', 'unit', 'unit', 'unit') ?>
  </div>

  
  <?= button('submit', 'unit', 'sendUnit', 'btn', 'Submit Unit', []) ?>

  <?php if($error): ?>
    <div class="alert"><?= $error ?></div>
  <?php endif ?>
</form>

<?php endif ?>

What about the fields types you want to update? Does the data fit the field types?

This won’t give you anything, because you are using an undefined constant here.

Yeah i caught that, i just followed some code from another forum post. I replaced the constant with the $e and get error message through now.

i finally got it working a little while ago… seems it was down to duplicate IDs in my form.

Yeah, well, hard to tell from something like that where you can only guess what is what…

its my own plugin too!! :face_with_open_eyes_and_hand_over_mouth:

I should have read my own docs.

:rofl:

But maybe make a video, given that nobody seems to read anymore.

No. I tend to stutter. Would confuse people even more!