* Set a field's value with type checking.
(field: FormField, value: FieldValue)
| 1379 | * Set a field's value with type checking. |
| 1380 | */ |
| 1381 | private setFieldValue(field: FormField, value: FieldValue): void { |
| 1382 | if (field instanceof TextField) { |
| 1383 | if (typeof value !== "string") { |
| 1384 | throw new TypeError( |
| 1385 | `Text field "${field.name}" requires string value, got ${typeof value}`, |
| 1386 | ); |
| 1387 | } |
| 1388 | |
| 1389 | field.setValue(value); |
| 1390 | |
| 1391 | return; |
| 1392 | } |
| 1393 | |
| 1394 | if (field instanceof CheckboxField) { |
| 1395 | if (typeof value === "boolean") { |
| 1396 | if (value) { |
| 1397 | field.check(); |
| 1398 | } else { |
| 1399 | field.uncheck(); |
| 1400 | } |
| 1401 | |
| 1402 | return; |
| 1403 | } |
| 1404 | |
| 1405 | if (typeof value === "string") { |
| 1406 | field.setValue(value); |
| 1407 | |
| 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | throw new TypeError(`Checkbox "${field.name}" requires boolean or string value`); |
| 1412 | } |
| 1413 | |
| 1414 | if (field instanceof RadioField) { |
| 1415 | if (typeof value !== "string" && value !== null) { |
| 1416 | throw new TypeError(`Radio field "${field.name}" requires string or null value`); |
| 1417 | } |
| 1418 | |
| 1419 | field.setValue(value); |
| 1420 | |
| 1421 | return; |
| 1422 | } |
| 1423 | |
| 1424 | if (field instanceof DropdownField) { |
| 1425 | if (typeof value !== "string") { |
| 1426 | throw new TypeError(`Dropdown "${field.name}" requires string value`); |
| 1427 | } |
| 1428 | |
| 1429 | field.setValue(value); |
| 1430 | |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | if (field instanceof ListBoxField) { |
| 1435 | if (!Array.isArray(value)) { |
| 1436 | throw new TypeError(`Listbox "${field.name}" requires string[] value`); |
| 1437 | } |
| 1438 |