I make use of the front-end entry form for an event registration process. The event is the main entry, and every event registration an entry of a related registration section. The challenge is to see whether a user has already been registered for an event. Today, I do that with a database query before the entry form loads. But for some reason, this works not always (for example when users reload the page). How could I make the combination of two fields ("user id" and "registration status") unique? (Normally, I would do that on a database level with a unique index over the two fields.)

  • You could get entries for the specific section by their authorId and if they have one don't load the form else load the form and let them register. – mcclaskiem Apr 14 '16 at 11:59
  • That is exactly what I do today. But nevertheless, I get duplicated entries from time to time. – MISC Apr 14 '16 at 13:48
  • If there is only one entry from a user in a specific section you should only see one entry come back when you query based on the authorId, what does your template look like? – mcclaskiem Apr 14 '16 at 13:55
  • No, there is more than one entry from a user in a section. – MISC Apr 14 '16 at 17:48
  • I will now add a text field containing string composed of event_id and user_id and make this field value unique using the following plugin: github.com/joshangell/craft-unique-value – MISC Apr 15 '16 at 11:52

If I understood you correctly, you have two sections:

  • events
  • registrations

Is the user logged in? If yes, you can save the user id in your registration entry. In your events template you can check now, if a registration entry exists that is related to this event and the current user. See example code (untested).

{% if currentUser %}

   {% set query = "userIdField:"~ currentUser.id %}
   {% set results = craft.entries({
      relatedTo: entry.id,
      search: query
   }) %}

{% endif %}

{% if results|length %}
    user is already registered
{% else %}
    show registration form
{% endif%}

If your user is not logged in, you could (temporarily) store the registration information in a cookie (check out this plugin: https://github.com/lewisjenkins/craft-lj-cookies) and do a similar check.

  • Yes, the user logged in and your proposal is more or less what I do already. The problem is that I still get duplicated entries from time to time. – MISC Apr 15 '16 at 11:46
  • Perhaps the user press the registration button twice, or he use the back button. In certain circumstances this could create a second entry. To avoid the "double click" you could disable the button with javascript. Have you activated the CSRF Protection -> craftcms.com/support/csrf-protection? Hope this helps. – Tom Bauer Apr 15 '16 at 14:41
  • No, CSRF protection has not been enabled yet. I will try that. Thank you very much. – MISC Apr 16 '16 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.