Allow users to only submit form once


#1

I’m building a system for tracking RSVPs for an event and everything is going great so far. I’m creating frontend-only users than can submit a form which then updates a structure field. I’d like to implement a check so that if a user has already submitted the form, they cannot again when they log in (do not show the form at all), but I can’t seem to work it out.

The usernames are being written to the YAML as part of the form submission, is there a way I can check the current active user against submitted usernames? Or is my thinking totally wrong on this?

I am using this recipe for the form submission: https://getkirby.com/docs/cookbook/creating-pages-from-frontend

Thanks for any help!
Jamie


#2

Well, you could probably just set the contents of the Structured field into an array and then use in_array in combination with the user login check to set the disabled status on the submit button. In other words, check the current user is not in the array, else enable the button. You could even hide the form completely and display a “Hey, you replied already!” message instead.


#3

I agree with @jimbobrjames, here is some code to get you going:

$users = array_column($page->name_of_structurefield()->yaml(), 'name_of_field_that_contains_username');
$username = '';
if($user = $site->user()) {
  $username = $user->username();
}
if(!in_array($username, $users)) {
  // display your form
} else {
  echo 'You have already replied';
}

If you need this logic more than once, it might make sense to wrap it in a function that you can then call anywhere.


#4

Perfect! Thank you @texnixe and @jimbobrjames!