### Problem/Motivation
`WebformBooking::validateSlotAvailability()` is attached as an
`#element_validate` callback of the booking element. Webform only
validates elements on the current wizard page; elements on other pages
have `#access` set to FALSE and their validators do not run.
When the booking element is on an earlier page than the submit button,
capacity is checked only when the visitor leaves that page. It is never
checked again on completion. Two visitors can select the last free seat,
continue through the remaining pages and both complete the submission,
so the slot ends up overbooked.
Even when the element is on the final page, the check and the save are
not atomic, so two near-simultaneous completions can both pass.
### Steps to reproduce
1. Create a webform with two wizard pages. Put a Webform Booking element
with "Seats per slot" = 1 on page 1 and any field on page 2.
2. In browser A, select a slot and go to page 2.
3. In browser B (another session), select the same slot and go to page 2.
4. Submit in browser A, then in browser B.
5. Both submissions are saved for the same slot.
### Proposed resolution
Re-check capacity when the submission completes, whichever page holds
the element, and serialise concurrent completions:
- New `SlotCapacityGuard` service (`webform_booking.slot_capacity_guard`),
attached in `webform_booking_form_alter()` to `$form['#validate']` and to
every action button that declares its own `#validate`.
- It runs only when the triggering element runs `::complete` (the same
detection as `PaymentVerification`), so wizard next/previous and drafts
are unaffected.
- For each booking element it reads the value from the form state, falling
back to the submission entity for elements on earlier pages, and reuses
`WebformBooking::validateSlotAvailability()`, so capacity, seat limits,
consecutive runs and exclusion of the edited submission stay in one place.
- Before checking, it acquires a lock named
`webform_booking:::` (waiting up to 15 s for a
concurrent holder). The lock is released by the lock backend at the end
of the request, after the submission is committed.
- Kernel test `SlotCapacityGuardTest` covers: full slot refused on
completion, free slot accepted, non-completing triggers ignored, editing
one's own booking accepted, lock held elsewhere refused, and attachment
to buttons.
Patch attached, made against 1.2.1.
### Remaining tasks
- Review.
- Run the kernel tests in CI. The patch was verified manually on a
production-like site with two browsers.
### User interface changes
When the slot fills up between selecting it and completing, the visitor
sees the existing message "The selected time slot is no longer available
for the full duration requested." on completion. If the lock cannot be
acquired, they see "Too many bookings are being made right now. Please
submit the form again."
### API changes
New service `webform_booking.slot_capacity_guard`. No changes to existing
public methods.
| Comment | File | Size | Author |
|---|---|---|---|
| webform_booking-1.2.1-slot-capacity-on-complete-v2.patch | 16.82 KB | adams39 |
Comments
Comment #3
rfmarcelino commentedThanks @adams39 for the report and the patch — the analysis is right. #element_validate only runs on the wizard page that holds the element, so on a multi-page form the slot was validated when the visitor left that page and never again.
I have committed a reworked version of your approach to 1.2.x. The structure is yours (SlotCapacityGuard service, ::complete detection shared with PaymentVerification, re-use of WebformBooking::validateSlotAvailability(), a per webform/element/day lock, kernel test coverage), with these changes:
1. The guard is only attached to $form['#validate']. attach() also appended the callback to every action button that declares its own #validate. That is the right thing to do in core, but Webform compensates for exactly this: WebformSubmissionForm::validateForm() replays $form['#validate'] itself when the triggering element has its own handlers. The guard therefore already ran once through that replay, and the button copy was a second invocation that only the DONE temporary value suppressed. It also landed on wizard_prev / preview_prev, whose #validate is ['::noValidate'].
2. Lock contention no longer refuses the booking. Refusing on a failed acquire() is risky on a paying webform: the guard runs before webform_booking.payment_verification, so the error sends payment verification down its "payment was not applied, contact the site with this reference" branch. The visitor would be told to submit the form again while the captured PayPal order is left unclaimed — a second charge or a support ticket, for a slot that may well have been free. Now, when the lock cannot be had the capacity check still runs, unserialised (which is the pre-1.2.x behaviour, so no regression), and the fact is logged. A genuinely full slot is still refused, with or without the lock.
3. The lock wait is short. wait($name, 15) blocked the request for up to 15 s per booking element, while holding the locks already taken for earlier elements. On a popular day that ties up a PHP worker per submit. It is now three tries with a 1 s wait, and the lock is released immediately when the booking is refused, since nothing will be saved.
4. The widget is marked again. validateSlotAvailability() signals the JS widget through $element['#attached']['drupalSettings']. The guard passes a throwaway element array, so that was discarded and the calendar still showed the slot as happily selected. The same setting is now written at the form root, where drupalSettings is merged all the same.
5. Refusals are logged, like every PaymentVerification refusal path, so maintainers can tell lock contention from genuinely full slots.
6. Programmatic submissions are skipped ($form_state->isProgrammed()), matching PaymentVerification.
Tests: the kernel test is extended to cover the widget marking, programmatic submissions, both lock-contention outcomes, and — importantly — that the guard actually runs through Webform's $form['#validate'] replay rather than only when called directly. 190 tests / 1328 assertions pass on the module.
Remaining known limits, documented in the class docblock: the serialisation window is bounded by the 30 s lock timeout, and the lock is released by the lock backend at the end of the request, after the submission is committed. The PayPal create/capture endpoints keep their own availability check.
Comment #5
adams39 commentedThanks, updated to 1.2.3 and confirmed the fix on our site.