Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-06T11:49:16.507Z
Answers.Text: Will give this code a go later on, but this is the kind of thing I was looking for, thank you.
Answers.UserDisplayName: darylknight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-06T11:52:10.833Z
Answers.Text: 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.
Answers.UserDisplayName: darylknight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-06T12:07:30.637Z
Answers.Text: Why not? Try `{% if similarBooking|length <= 5 %}` for max. 5 bookings.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-17T11:43:38.887Z
Answers.Text: 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.
Answers.UserDisplayName: darylknight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-17T15:08:31.400Z
Answers.Text: @Daryl I edited the answer, hope this helps!
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-29T13:05:43.860Z
Answers.Text: 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.
Answers.UserDisplayName: darylknight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T21:08:22.683Z
Answers.LastActivityDate: 2014-09-29T17:23:04.553Z
Answers.LastEditDate: 2014-09-29T17:23:04.553Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-06T11:49:41.273Z
Answers.Text: Thanks, but I can't write plugins so that one's a no-go. Needed to be done with template code.
Answers.UserDisplayName: darylknight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T18:27:43.343Z
Answers.LastActivityDate: 2014-09-04T18:27:43.343Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Tags: <form><entry>
Title: How can I stop a duplicate entry being created & provide feedback?
ThreadText: # 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:
  
  ```
  <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?
  
  **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 #}
              <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 %}
  
  ```
  
  > **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 username_1, 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 username_1 - 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.  
  
  ---
  
  