3

I have a booking form on my site, which when submitted creates a new entry which has two fields: an Entries field which links to a single User, and another Entries field which links to a single Event.

The title field of the entry is set automatically to "[date of event] - [username]".

The problem is that some users are booking twice for the same event. We need a way to limit bookings to ONE PER USER. Is there a way to prevent an entry with the same title being created, and rather than just failing to submit the form, give the user an error message saying "you can't book more than one place"?

Form:

<form method="post" action="" accept-charset="UTF-8" class="custom">
    <input type="hidden" name="action" value="entries/saveEntry">
    <input type="hidden" name="redirect" value="events/book/complete/{{eventID}}">
    <input type="hidden" name="sectionId" value="18">
    <input type="hidden" name="enabled" value="1">
    <input type="hidden" name="fields[userBooking][]" value="{{userID}}">
    <input type="hidden" name="fields[eventBooking][]" value="{{eventID}}">
    <input type="submit" value="Confirm Booking" class="button secondary">
</form>

Related question (possibly could be solved with the same answer) - I need to limit the number of bookings an event can take in total. So for example:

  • Event One: Bookings per user: 1, Total places: 35
  • Event Two: Bookings per user: 1, Total places: 50

This needs to be done with template code, not a plugin. Any ideas?

2 Answers 2

2

What about not showing the submit button at all, if a similar booking already exists?

{% if currentUser|length %}
    {% if entry.dateTime > now %} {# If event is in the future, display button to book #}

        {% set similarBooking = craft.entries({ sectionId: '18', relatedTo: ['and', { targetElement: currentUser, field: 'userBooking' }, { targetElement: entry.id, field: 'eventBooking' } ]}) %}
        {% set totalBookings = craft.entries({ sectionId: '18', relatedTo: { targetElement: entry.id, field: 'eventBooking' } }) %}

        {% if similarBooking|length %} {# Check if they have already booked #}
            <p>You have already booked a place for this event.</p>
        {% else %} {# No previous booking, so check if there are spaces #}

            {% if entry.maxBookings > 0 %} {# Check if there is a limit on spaces #}

                {% if totalBookings|length < entry.maxBookings %} {# Check if there are spaces left #}
                    <a href="#">Book Now</a>
                {% else %} {# There are no spaces left #}
                    <p>This event is full.</p>
                {% endif %}

            {% else %} {# No limit, display booking button #}
                <a href="#">Book Now</a>
            {% endif %}

        {% endif %}

    {% endif %}

{% else %}
    <a href="#">Login to book</a>
{% endif %}
6
  • Will give this code a go later on, but this is the kind of thing I was looking for, thank you. Sep 6, 2014 at 11:49
  • I'm guessing this could probably be adapted in some way to put a limit on bookings too. If I can count the number of bookings that match that date, and when it's over a certain number, replace the book button with a "this event is full" message. Sep 6, 2014 at 11:52
  • Why not? Try {% if similarBooking|length <= 5 %} for max. 5 bookings.
    – carlcs
    Sep 6, 2014 at 12:07
  • Hi carlcs, I've just come back to this and realised the question has been misunderstood (I've edited to reword). Multiple users are allowed to book on the event, but only once each. I need to stop a single user booking two places. Sep 17, 2014 at 11:43
  • @Daryl I edited the answer, hope this helps!
    – carlcs
    Sep 17, 2014 at 15:08
1

Write a plugin, and have that plugin do the custom validation for you.

1
  • Thanks, but I can't write plugins so that one's a no-go. Needed to be done with template code. Sep 6, 2014 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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