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

<pre><code>{% if currentUser|length %}
    {% if entry.dateTime &gt; 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 #}
            &lt;p&gt;You have already booked a place for this event.&lt;/p&gt;
        {% else %} {# No previous booking, so check if there are spaces #}

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

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

            {% else %} {# No limit, display booking button #}
                &lt;a href="#"&gt;Book Now&lt;/a&gt;
            {% endif %}

        {% endif %}

    {% endif %}

{% else %}
    &lt;a href="#"&gt;Login to book&lt;/a&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-09-06T11:49:16.507Z
Will give this code a go later on, but this is the kind of thing I was looking for, thank you.
darylknight
CC BY-SA 3.0
2014-09-06T11:52:10.833Z
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.
darylknight
CC BY-SA 3.0
2014-09-06T12:07:30.637Z
Why not? Try `{% if similarBooking|length <= 5 %}` for max. 5 bookings.
carlcs
CC BY-SA 3.0
2014-09-17T11:43:38.887Z
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.
darylknight
CC BY-SA 3.0
2014-09-17T15:08:31.400Z
@Daryl I edited the answer, hope this helps!
carlcs
CC BY-SA 3.0
2014-09-29T13:05:43.860Z
Hey carlcs - I've updated your answer with the code that worked, couldn't have got there without you. Thank you! This now checks for duplicate bookings and limits the number of available places.
darylknight
CC BY-SA 3.0
2014-09-04T21:08:22.683Z
2014-09-29T17:23:04.553Z
2014-09-29T17:23:04.553Z
carlcs
<p><a href="http://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">Write a plugin</a>, and have that plugin do the custom validation for you.</p>

CC BY-SA 3.0
2014-09-06T11:49:41.273Z
Thanks, but I can't write plugins so that one's a no-go. Needed to be done with template code.
darylknight
CC BY-SA 3.0
2014-09-04T18:27:43.343Z
2014-09-04T18:27:43.343Z
Marion Newlevant
<p>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.</p>

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

<p>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"?</p>

<p>Form:</p>

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

<p>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:</p>

<ul>
<li>Event One: Bookings per user: 1, Total places: 35</li>
<li>Event Two: Bookings per user: 1, Total places: 50</li>
</ul>

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

<form><entry>
How can I stop a duplicate entry being created & provide feedback?
# How can I stop a duplicate entry being created & provide feedback?

(Asked by: username_0 on 2014-09-04)

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:

    
    
    
    
    
    
    


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?

**Tags:** form, entry

---

## 2 answers

### Answer by username_1 on 2014-09-04. Score: 2 | Accepted answer

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 #}
            You have already booked a place for this event.
        {% 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 #}
                    Book Now
                {% else %} {# There are no spaces left #}
                    This event is full.
                {% endif %}

            {% else %} {# No limit, display booking button #}
                Book Now
            {% endif %}

        {% endif %}

    {% endif %}

{% else %}
    Login to book
{% endif %}

> **Comments:**
> **username_0**: Will give this code a go later on, but this is the kind of thing I was looking for, thank you.  
> **username_0**: 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.  
> **username_1**: Why not? Try `{% if similarBooking|length <= 5 %}` for max. 5 bookings.  
> **username_0**: 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.  
> **username_1**: @Daryl I edited the answer, hope this helps!  
> **username_0**: Hey carlcs - I've updated your answer with the code that worked, couldn't have got there without you. Thank you! This now checks for duplicate bookings and limits the number of available places.  

---

### Answer by username_2 on 2014-09-04. Score: 1

[Write a plugin](http://buildwithcraft.com/docs/plugins/introduction), and have that plugin do the custom validation for you.

> **Comments:**
> **username_0**: Thanks, but I can't write plugins so that one's a no-go. Needed to be done with template code.  

---

