Hook user.update:after

The user should be able to change the time on frontend. The problem is, the time is changing but on the frontend shows up the old value. After a refresh[“F5”] the value is correct!

There must be a hook(user.update:after) somewhere, i know, but where?

Templatefile:

<form method="post">
<input name="start_mo" type="time" min="07:00" max="21:00"  value="<?=$kirby->user()->start_mo()?>">
<input class="btn" type="submit" name="speichern" value="Speichern">
</form>

Controllerfile:

<?php
return function($kirby, $page) {

if($kirby->request()->is('POST')) {
  if(get('speichern')){
    $data = array(
      'start_mo'  => esc(get('start_mo')),
       ...
    );
    $rules = array(
       'start_mo' => array('required','time'),
       ...
    );
    $messages = array(
       'start_mo' => 'Gib eine gültige Start-Zeit für Montagmorgen ein.',
       ...
    );
if($invalid = invalid($data, $rules, $messages)) {
  $error = $invalid;
}else{
	try {
		$kirby->user()->update([
			'start_mo' => $data['start_mo'],
                         ...
                ]);
        $success = 'Deine Sperrzeiten sind aktualisiert.';

	} catch(Exception $e) {
		$error = 'Fehler beim Speichern der Sperrzeiten!<br>'.$e;
	}
	}
}
else{ $error = null;}
}
    return compact('user', 'error', 'success');
};

Not quite sure what you are trying to achieve here.

So the frontend displays something and you want to update what is displayed via a form. But unless you insert the new value into JavaScript/Ajax, you have to reload the page, that is normal.

Or am I misunderstanding/missing something?

A hook only changes your backend stuff, not your frontend.

The frontend display shows a value in an input field which is in a form and the form action url is the same page.
So if the user press submit the value should be writen in the content file of the user.
After that, the same page is loading, thats correct, but with the old value and thats the failure!

I think the problem is that you are not using your $user variable, although you are passing it to the template.

<form method="post">
<input name="start_mo" type="time" min="07:00" max="21:00"  value="<?= $user->start_mo()?>">
<input class="btn" type="submit" name="speichern" value="Speichern">
</form>

And in your controller, you have to assign the updated user to the $user variable.

<?php
return function($kirby, $page) {

$user = $kirby->user();
if($kirby->request()->is('POST')) {
  if(get('speichern')){
    $data = array(
      'start_mo'  => esc(get('start_mo')),
       ...
    );
    $rules = array(
       'start_mo' => array('required','time'),
       ...
    );
    $messages = array(
       'start_mo' => 'Gib eine gültige Start-Zeit für Montagmorgen ein.',
       ...
    );
if($invalid = invalid($data, $rules, $messages)) {
  $error = $invalid;
}else{
	try {
		$user = $user->update([
			'start_mo' => $data['start_mo'],
                         ...
                ]);
        $success = 'Deine Sperrzeiten sind aktualisiert.';

	} catch(Exception $e) {
		$error = 'Fehler beim Speichern der Sperrzeiten!<br>'.$e;
	}
	}
}
else{ $error = null;}
}
    return compact('user', 'error', 'success');
};

Sorry for getting back so late, I somehow missed that this question was still unanswered.

1 Like

Thanks a lot! This is the solution! :see_no_evil: :smiley: :+1:

$user = $user->update([ start_mo' => $data['start_mo']]);