Problem/Motivation

When a Webform Booking element is marked as required and has a visibility condition (e.g., visible only when another field has a specific value), the required validation is bypassed. Users can submit the form without selecting a day/time slot in the booking calendar.

Steps to reproduce

1. Create a webform with a select/radios field and a Booking element.
2. Mark the Booking element as required.
3. Add a visibility condition on the Booking element (e.g., visible when the select field equals a specific value).
4. Visit the form, select the triggering value so the booking element becomes visible.
5. Submit the form without selecting a slot in the booking calendar.
6. Expected: Validation error saying the booking field is required.
7. Actual: Form submits successfully without any error.

Note: When the Booking element is unconditionally required (no visibility condition), the validation works correctly.

Proposed resolution

Two changes to fix both issues:

1. **`src/Plugin/WebformElement/WebformBooking.php`** — Update `validateEmptySlot()` to check both `$element['#required']` and `$element['#_required']`, so conditional required (via visibility states) is respected.

2. **`src/Element/WebformBooking.php`** — Add a `setRequiredError()` static method. This is the hook that `WebformSubmissionConditionsValidator` calls (via `method_exists`) to let custom elements handle their own required validation. The method checks whether the `slot` value is actually empty and sets the appropriate form error.

Remaining tasks

Review and test.

User interface changes

None.

API changes

A new public static method `WebformBooking::setRequiredError()` is added to the Element class (`src/Element/WebformBooking.php`).

Data model changes

None.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

aren33k created an issue. See original summary.

aren33k’s picture

Assigned: aren33k » Unassigned
Status: Active » Needs review

Code changes were made, ready to be reviewed.

rfmarcelino’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @aren33k
Reproduced the issue and tested the patch.
Merged

aren33k’s picture

@rfmarcelino thanks!

aren33k’s picture

Status: Reviewed & tested by the community » Fixed

@rfmarcelino I’m setting the status to fixed.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.