Allow users to only submit form once

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:

Thanks for any help!

1 Like

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.

1 Like

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.

1 Like

Perfect! Thank you @texnixe and @jimbobrjames!