Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FORMS-14814: allow multiple file attachment #191

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion blocks/form/components/file/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,26 @@ function fileValidation(input, files) {
const maxItems = (parseInt(input.dataset.maxItems, 10) || -1);
const fileSize = `${input.dataset.maxFileSize || '2MB'}`;
let constraint = '';
let constraintErrorMessage = '';
let errorMessage = '';
const wrapper = input.closest('.field-wrapper');
if (!checkAccept(acceptedFile, files)) {
constraint = 'accept';
constraintErrorMessage = 'acceptErrorMessage';
} else if (!checkMaxFileSize(fileSize, files)) {
constraint = 'maxFileSize';
constraintErrorMessage = 'maxFileSizeErrorMessage';
} else if (multiple && maxItems !== -1 && files.length > maxItems) {
constraint = 'maxItems';
constraintErrorMessage = 'maxItemsErrorMessage';
errorMessage = defaultErrorMessages.maxItems.replace(/\$0/, maxItems);
} else if (multiple && minItems !== 1 && files.length < minItems) {
constraint = 'minItems';
constraintErrorMessage = 'minItemsErrorMessage';
errorMessage = defaultErrorMessages.minItems.replace(/\$0/, minItems);
}
if (constraint.length) {
const finalMessage = wrapper.dataset[constraint]
const finalMessage = wrapper.dataset[constraintErrorMessage]
|| errorMessage
|| defaultErrorMessages[constraint];
input.setCustomValidity(finalMessage);
Expand Down
45 changes: 31 additions & 14 deletions blocks/form/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
import GoogleReCaptcha from './integrations/recaptcha.js';
import componentDecorator from './mappings.js';
import DocBasedFormToAF from './transform.js';
import transferRepeatableDOM from './components/repeat/repeat.js';
import transferRepeatableDOM, { insertAddButton, insertRemoveButton } from './components/repeat/repeat.js';
import { handleSubmit } from './submit.js';
import { getSubmitBaseUrl, emailPattern } from './constant.js';
import { createOptimizedPicture } from '../../scripts/aem.js';

export const DELAY_MS = 0;
let captchaField;
Expand All @@ -33,7 +34,7 @@ function setPlaceholder(element, fd) {
const constraintsDef = Object.entries({
'password|tel|email|text': [['maxLength', 'maxlength'], ['minLength', 'minlength'], 'pattern'],
'number|range|date': [['maximum', 'Max'], ['minimum', 'Min'], 'step'],
file: ['accept', 'Multiple'],
file: ['accept', 'Multiple', 'maxItems', 'minItems'],
panel: [['maxOccur', 'data-max'], ['minOccur', 'data-min']],
}).flatMap(([types, constraintDef]) => types.split('|')
.map((type) => [type, constraintDef.map((cd) => (Array.isArray(cd) ? cd : [cd, cd]))]));
Expand Down Expand Up @@ -149,6 +150,23 @@ function createLegend(fd) {
return createLabel(fd, 'legend');
}

function createRepeatablePanel(wrapper, fd) {
setConstraints(wrapper, fd);
wrapper.dataset.repeatable = true;
wrapper.dataset.index = fd.index || 0;
if (fd.properties) {
Object.keys(fd.properties).forEach((key) => {
if (!key.startsWith('fd:')) {
wrapper.dataset[key] = fd.properties[key];
}
});
}
if ((!fd.index || fd?.index === 0) && fd.properties?.variant !== 'noButtons') {
insertAddButton(wrapper, wrapper);
insertRemoveButton(wrapper, wrapper);
}
}

function createFieldSet(fd) {
const wrapper = createFieldWrapper(fd, 'fieldset', createLegend);
wrapper.id = fd.id;
Expand All @@ -157,9 +175,7 @@ function createFieldSet(fd) {
wrapper.classList.add('panel-wrapper');
}
if (fd.repeatable === true) {
setConstraints(wrapper, fd);
wrapper.dataset.repeatable = true;
wrapper.dataset.index = fd.index || 0;
createRepeatablePanel(wrapper, fd);
}
return wrapper;
}
Expand All @@ -184,7 +200,12 @@ function createRadioOrCheckboxGroup(fd) {
enum: [value],
required: fd.required,
});
const layout = fd.properties['afs:layout'];
const { variant, 'afs:layout': layout } = fd.properties;
if (variant === 'cards') {
wrapper.classList.add(variant);
} else {
wrapper.classList.remove('cards');
}
if (layout?.orientation === 'horizontal') {
wrapper.classList.add('horizontal');
}
Expand Down Expand Up @@ -228,14 +249,10 @@ function createPlainText(fd) {

function createImage(fd) {
const field = createFieldWrapper(fd);
const imagePath = fd.source || fd.properties['fd:repoPath'] || '';
const image = `
<picture>
<source srcset="${imagePath}?width=2000&optimize=medium" media="(min-width: 600px)">
<source srcset="${imagePath}?width=750&optimize=medium">
<img alt="${fd.altText || fd.name}" src="${imagePath}?width=750&optimize=medium">
</picture>`;
field.innerHTML = image;
field.id = fd?.id;
const imagePath = fd.value || fd.properties['fd:repoPath'] || '';
const altText = fd.altText || fd.name;
field.append(createOptimizedPicture(imagePath, altText));
return field;
}

Expand Down
2 changes: 1 addition & 1 deletion component-definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,4 @@
]
}
]
}
}
63 changes: 63 additions & 0 deletions component-models.json
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,13 @@
}
]
},
{
"component": "boolean",
"name": "multiSelection",
"label": "Allow multiple file attachments",
"valueType": "boolean",
"collapsible": false
},
{
"component": "container",
"name": "help",
Expand Down Expand Up @@ -1798,6 +1805,62 @@
"valueType": "string",
"description": "Displayed when validation script fails."
},
{
"component": "number",
"name": "minItems",
"label": "Minimum number of files",
"valueType": "string",
"condition": {
"==": [
{
"var": "multiSelection"
},
true
]
}
},
{
"component": "text",
"name": "minItemsMessage",
"label": "Minimum files error message",
"valueType": "string",
"condition": {
"==": [
{
"var": "multiSelection"
},
true
]
}
},
{
"component": "number",
"name": "maxItems",
"label": "Maximum number of files",
"valueType": "string",
"condition": {
"==": [
{
"var": "multiSelection"
},
true
]
}
},
{
"component": "text",
"name": "maxItemsMessage",
"label": "Maximum files error message",
"valueType": "string",
"condition": {
"==": [
{
"var": "multiSelection"
},
true
]
}
},
{
"component": "number",
"name": "maxFileSize",
Expand Down
63 changes: 63 additions & 0 deletions models/form-components/_file-input.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
"collapsible": false,
"...": "../form-common/_basic-input-fields.json"
},
{
"component": "boolean",
"name": "multiSelection",
"label": "Allow multiple file attachments",
"valueType": "boolean",
"collapsible": false
},
{
"...": "../form-common/_help-container.json"
},
Expand All @@ -50,6 +57,62 @@
{
"...": "../form-common/_script-validation-fields.json#/fields"
},
{
"component": "number",
"name": "minItems",
"label": "Minimum number of files",
"valueType": "string",
"condition": {
"==": [
{
"var": "multiSelection"
},
true
]
}
},
{
"component": "text",
"name": "minItemsMessage",
"label": "Minimum files error message",
"valueType": "string",
"condition": {
"==": [
{
"var": "multiSelection"
},
true
]
}
},
{
"component": "number",
"name": "maxItems",
"label": "Maximum number of files",
"valueType": "string",
"condition": {
"==": [
{
"var": "multiSelection"
},
true
]
}
},
{
"component": "text",
"name": "maxItemsMessage",
"label": "Maximum files error message",
"valueType": "string",
"condition": {
"==": [
{
"var": "multiSelection"
},
true
]
}
},
lovelymandal16 marked this conversation as resolved.
Show resolved Hide resolved
{
"component": "number",
"name": "maxFileSize",
Expand Down
Loading