# Content controls and forms

> Build fillable documents with content controls, form mode, form data, validation and editing restrictions in Birch UI Document Editor.

A content control is a named region of a document that holds one value: a customer name, a delivery option, a date, a tick box. Controls are Word structured document tags, so a form built here opens as a form in Word and the other way round.

## Insert controls

```ts
editor.exec('insertContentControl', {
  kind: 'plainText',
  tag: 'customer',
  title: 'Customer',
  placeholder: 'Enter the customer',
  required: true,
});
editor.exec('insertContentControl', {
  kind: 'dropdown',
  tag: 'delivery',
  title: 'Delivery',
  items: [
    { text: 'Standard (3 days)', value: 'standard' },
    { text: 'Next day', value: 'express' },
  ],
});
editor.exec('insertContentControl', { kind: 'date', tag: 'start', dateFormat: 'd MMMM yyyy' });
editor.exec('insertContentControl', { kind: 'checkbox', tag: 'accepted', checked: false });
```

The kinds are `plainText`, `richText`, `dropdown`, `comboBox`, `date`, `checkbox`, `picture` and `repeatingSection`. `lockContent` stops the value from being edited and `lockControl` stops the control from being deleted. **Insert → Content controls** offers the same list, `exec('contentControlProperties')` opens the properties dialog for the control at the cursor, and `exec('removeContentControl')` removes the control and keeps its text.

In document JSON a control is an `sdt_inline` or `sdt_block` node with a `control` attribute of type `ContentControl`.

## Form mode

```ts
const editor = createDocumentEditor(host, { content: orderForm, formMode: true });
editor.setFormMode(false); // back to designing the form
editor.isFormMode();
```

In form mode only the content of controls can be changed. <kbd>Tab</kbd> and <kbd>Shift</kbd>+<kbd>Tab</kbd> move between controls in document order, and <kbd>Alt</kbd>+<kbd>Down</kbd> opens the list of a drop-down control, where the arrow keys and <kbd>Enter</kbd> choose an item. **Review → Fill in form** switches the mode from the ribbon.

## Read, fill and validate

```ts
editor.getFormData(); // { customer: '', delivery: 'express', … } keyed by tag
editor.setFormData({ customer: 'Harper & Lowe LLP', accepted: true }); // returns the number of controls changed
editor.validateForm(); // [{ tag: 'customer', title: 'Customer', message: 'Customer is required.' }]
editor.on('formChange', ({ tag, value, data }) => saveDraft(data));
```

Values are keyed by `tag`. A control that still shows its placeholder reads as an empty string. `validateForm()` reports `required` controls that are empty.

`required` and a custom `placeholder` text live in the document JSON only. Word has no equivalent setting, so they are not written to DOCX; a document that returns from Word keeps its controls, tags, titles, list items and locks.

## Restrict editing

```ts
editor.exec('restrictEditing', 'comments'); // 'readOnly' | 'comments' | 'trackedChanges' | 'forms' | 'none'
editor.getProtection(); // { mode: 'comments', enforced: true } or null
```

| Mode | Effect |
| --- | --- |
| `readOnly` | No edits; the editor reports `isReadOnly()`. |
| `comments` | The text cannot be changed; comments can be added. |
| `trackedChanges` | Track changes is switched on and cannot be switched off. |
| `forms` | Form mode is switched on. |

The protection of an imported document is enforced when it opens and is written back on export. Without a password the restriction is a setting for cooperating users: `exec('restrictEditing', 'none')` removes it.

```ts
await editor.protectDocument('forms', 'Harbour-2026'); // any mode, with or without a password
editor.exec('restrictEditing', 'none'); // false while a password is set
await editor.unprotectDocument('Harbour-2026'); // false when the password is wrong
editor.isPasswordProtected();
```

With a password, the restriction can only be changed through `unprotectDocument`, and a password that is already in an imported DOCX file is asked for in the same way. The hash is stored as Word stores it (salted SHA-512, 100,000 iterations). It is still not a security boundary: the content is not encrypted, and code with access to the document JSON can remove the setting.
