Skip to main content

DHTMLX Booking. Booking form fields example

Live example​

The booking dialog ends with a short form, and formShape defines what it asks for. Each entry names a comp, a key, and a label, and can add required, a validation function, and an errorMessage. This demo collects a name, an email, a phone number checked against a pattern, and a free text description.

const widget = new booking.Booking("#root", {
	start: buildToday(),
    data: dataset,

    formShape:[
        {
            comp: "text",
            key: "name",
            label: "Name",
            required: true,
        },
        {
            comp: "text",
            key: "email",
            label: "Email",
            required: true,
        },
        {
            comp: "text",
            key: "phone",
            label: "Phone",
            required: true,
            validation: val => {
                const regEx = /^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{3,4})(?: *x(\d+))?\s*$/g;
                return val && regEx.test(val);
            },
            errorMessage: " should contain a valid phone number (e.g. +1 234-567-8900)",
        },
        {
            comp: "textarea",
            key: "description",
            label: "Description",
        },
    ]
});

widget.setConfirmHandler(function(event){
    const { confirm } = event;
    setTimeout(() => {
        Math.random() < 0.5 ? confirm.error() : confirm.done();
    }, 1000);
});
<!-- component container -->
<div id="root" style="height:100%"></div>

<!-- dataset -->

<script>
function buildToday(){const params = new URLSearchParams(typeof location !== 'undefined' ? location.search : '');const fixed = params.get('fixedDate') || (typeof window !== 'undefined' && window.FIXED_DATE);return fixed ? new Date(fixed) : new Date();}

	const dataset = [
      {
        id: "ee828b5d-a034-420c-889b-978840015d6a",
        title: "Natalie Tyson",
        category: "Therapist",
        subtitle: "2 years of experience",
        details: "Cleveland Clinic\n9500 Euclid Ave",
        preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/01.jpg",
        price: "$35",
        review: {
            stars: 4,
            count: 120,
        },
        slots: [
            {
                from: 9,
                to: 20,
                days: [1, 2, 3, 4, 5],
            },
            {
                from: 10,
                to: 18,
                days: [6, 0],
            },
        ],
    },
      {
        id: "5c9b64ad-1830-4e5b-a5f9-8acea10706df",
        title: "James Anderson",
        category: "Allergist",
        subtitle: "3 years of experience",
        details: "UCLA Medical Center\n57 Westwood Plaza",
        preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/11.jpg",
        price: "$30",
        review: {
            stars: 4,
            count: 64,
        },
        slotSize: 45,
        slotGap: 10,
        slots: [
            {
                from: "9:15",
                to: 17,
                days: [1, 2, 3, 4, 5],
            },
        ],
    },
      // ... 44 more items (see Live Editor for full data)
    ];

function getDate(addDays, hoursValue = 0, minutesValue = 0) {
	const date = buildToday();
	const secondsValue = 0; // round to minutes
	const msValue = 0;

	date.setDate(date.getDate() + addDays);
	date.setHours(hoursValue, minutesValue, secondsValue, msValue);

	return date.getTime();
}
</script>

Use case​

What a business needs from the person booking is rarely name and email alone. A repair shop wants the plate number. An internal service desk wants the cost center, and a lab wants the case id that ties the visit to an existing record. Collecting it at this point is cheaper than chasing it afterwards, and a validation function keeps a mistyped number from reaching the queue at all. Fields are declared as data, so the same widget serves two departments by loading a different array.

How to do this in your app​

  • List the fields: formShape takes an array, and that array is the whole form, so include every field you want to keep.
  • Pick the control: comp is either "text" or "textarea".
  • Name the value: key identifies the field, and the filled form reaches your confirm handler as its data object.
  • Demand an answer: required: true blocks the confirmation while the field is empty.
  • Check the format: validation receives the value and returns true or false, and errorMessage is the text shown when it returns false.

Good to know​

  • Your array replaces the default one: the built-in name, email, and description fields are gone once formShape is set, so re-list the ones you still need.
  • The built-in checks come with the built-in fields: the default name and email entries carry their own validation and errorMessage, and a field you declare yourself validates only what your function tests.
  • Messages are printed after the label: the default texts start with a space, as in " should contain a valid phone number", so the dialog reads as one sentence.
  • The phone pattern is an example: it matches an optional country code and a number grouped as 3-3-3 or 3-3-4 digits, as in +1 234-567-8900. Another country needs its own expression.

FAQ​

How do you add a field to a booking form?​

Add an object to the formShape array with comp, key, and label. The array defines the whole form, so keep the default fields in it if you still want them.

How do you validate a phone number before a booking is confirmed?​

Give the field a validation function that tests the value against a regular expression and returns a boolean. Pair it with errorMessage for the text the visitor sees when the test fails.

Which field types does the booking form support?​

Two: "text" for a single line and "textarea" for a longer note. Both are set through the comp property of a field.

API reference​

Additional resources​