|
371 | 371 | const validateInput = (input) => {
|
372 | 372 | const isInputEmpty = !input.value;
|
373 | 373 | const field = input.closest('.form-group');
|
374 |
| - const labelNode = field.querySelector('.ibexa-label'); |
375 |
| - const errorNode = field.querySelector('.ibexa-form-error'); |
| 374 | + const labelNode = field?.querySelector('.ibexa-label'); |
| 375 | + const errorNode = field?.querySelector('.ibexa-form-error'); |
376 | 376 |
|
377 | 377 | input.classList.toggle('is-invalid', isInputEmpty);
|
378 | 378 |
|
|
390 | 390 |
|
391 | 391 | isEditFormValid = isEditFormValid && !isInputEmpty;
|
392 | 392 | };
|
| 393 | + const validateMatrixColumns = (columnSettingsNode) => { |
| 394 | + const columns = columnSettingsNode.querySelectorAll('.ibexa-matrix-settings__column'); |
| 395 | + const hasAddedColumns = columns.length > 0; |
| 396 | + const hasEmptyInputs = [...columns].some((column) => { |
| 397 | + const columnInput = column.querySelector('.ibexa-input--matrix-column-identifier'); |
| 398 | + |
| 399 | + return !columnInput.value; |
| 400 | + }); |
| 401 | + const isValid = hasAddedColumns && !hasEmptyInputs; |
| 402 | + const errorNode = columnSettingsNode.querySelector('.ibexa-form-error'); |
| 403 | + |
| 404 | + errorNode.toggleAttribute('hidden', hasAddedColumns); |
| 405 | + |
| 406 | + return isValid; |
| 407 | + }; |
393 | 408 | const validateForm = () => {
|
394 | 409 | const fieldDefinitionsStatuses = {};
|
| 410 | + const matrixColumnsSettingsNodes = doc.querySelectorAll('.ibexa-matrix-settings__columns'); |
395 | 411 |
|
396 | 412 | isEditFormValid = true;
|
397 | 413 | inputsToValidate = editForm.querySelectorAll(SELECTOR_INPUTS_TO_VALIDATE);
|
|
413 | 429 | validateInput(input);
|
414 | 430 | });
|
415 | 431 |
|
| 432 | + matrixColumnsSettingsNodes.forEach((columnSettingsNode) => { |
| 433 | + const fieldDefinition = columnSettingsNode.closest('.ibexa-collapse--field-definition'); |
| 434 | + const { fieldDefinitionIdentifier } = fieldDefinition.dataset; |
| 435 | + const hasError = !validateMatrixColumns(columnSettingsNode); |
| 436 | + |
| 437 | + if (!fieldDefinitionsStatuses[fieldDefinitionIdentifier]) { |
| 438 | + fieldDefinitionsStatuses[fieldDefinitionIdentifier] = []; |
| 439 | + } |
| 440 | + |
| 441 | + fieldDefinitionsStatuses[fieldDefinitionIdentifier].push(hasError); |
| 442 | + }); |
| 443 | + |
416 | 444 | Object.entries(fieldDefinitionsStatuses).forEach(([fieldDefinitionIdentifier, inputsStatus]) => {
|
417 | 445 | const isFieldDefinitionValid = inputsStatus.every((hasError) => !hasError);
|
418 | 446 | const fieldDefinitionNode = doc.querySelector(`[data-field-definition-identifier="${fieldDefinitionIdentifier}"]`);
|
|
629 | 657 | draggableGroups.push(draggable);
|
630 | 658 | });
|
631 | 659 |
|
| 660 | + doc.body.addEventListener('ibexa-fieldtype-matrix:added-column', (event) => { |
| 661 | + const { columnNode } = event.detail; |
| 662 | + const inputs = columnNode.querySelectorAll('.ibexa-input[required]'); |
| 663 | + |
| 664 | + [...inputs].forEach((input) => { |
| 665 | + attachValidateEvents(input); |
| 666 | + }); |
| 667 | + }); |
| 668 | + |
632 | 669 | fieldDefinitionsGroups.forEach((group) => group.addEventListener('click', () => setActiveGroup(group), false));
|
| 670 | + |
633 | 671 | inputsToValidate.forEach(attachValidateEvents);
|
634 | 672 |
|
635 | 673 | editForm.addEventListener(
|
|
0 commit comments