fix: radio and checkbox fields issues with empty values (#1273)

Since we allow checkboxes and radio fields without a label (which we use for the value) we 
had an issue where multiple checkboxes with no value would exist and items would not end
up checked on the resulting document.

This change fixes that by adding a placeholder value for these empty checkboxes and labels.
This commit is contained in:
Catalin Pit
2024-08-09 02:46:07 +02:00
committed by GitHub
parent e0065a8731
commit c605877924

View File

@ -133,9 +133,14 @@ export const insertFieldInPDF = async (pdf: PDFDocument, field: FieldWithSignatu
throw new Error('Invalid checkbox field meta');
}
const values = meta.data.values?.map((item) => ({
...item,
value: item.value.length > 0 ? item.value : `empty-value-${item.id}`,
}));
const selected = field.customText.split(',');
for (const [index, item] of (meta.data.values ?? []).entries()) {
for (const [index, item] of (values ?? []).entries()) {
const offsetY = index * 16;
const checkbox = pdf.getForm().createCheckBox(`checkbox.${field.secondaryId}.${index}`);
@ -169,9 +174,14 @@ export const insertFieldInPDF = async (pdf: PDFDocument, field: FieldWithSignatu
throw new Error('Invalid radio field meta');
}
const values = meta?.data.values?.map((item) => ({
...item,
value: item.value.length > 0 ? item.value : `empty-value-${item.id}`,
}));
const selected = field.customText.split(',');
for (const [index, item] of (meta.data.values ?? []).entries()) {
for (const [index, item] of (values ?? []).entries()) {
const offsetY = index * 16;
const radio = pdf.getForm().createRadioGroup(`radio.${field.secondaryId}.${index}`);