Skip to content

Commit 1ca2931

Browse files
committed
IBX-10362: No validation of matrix field without columns when creating content type
1 parent 2ea28df commit 1ca2931

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/bundle/Resources/public/js/scripts/admin.contenttype.matrix.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
const SELECTOR_ADD_COLUMN = '.ibexa-btn--add-column';
77
const SELECTOR_REMOVE_COLUMN = '.ibexa-btn--remove-column';
88
const SELECTOR_TEMPLATE = '.ibexa-matrix-settings__column-template';
9+
const ERROR_NODE_SELECTOR = '.ibexa-form-error';
910
const NUMBER_PLACEHOLDER = /__number__/g;
1011
const getNextIndex = (parentNode) => {
1112
return parentNode.dataset.nextIndex++;
@@ -36,9 +37,18 @@
3637
node.insertAdjacentHTML('beforeend', template.replace(NUMBER_PLACEHOLDER, getNextIndex(node)));
3738

3839
initColumns(settingsNode);
40+
validateColumnsNumber(settingsNode);
3941

4042
node.closest('.ibexa-table').dispatchEvent(new CustomEvent('ibexa-refresh-main-table-checkbox'));
43+
4144
doc.body.dispatchEvent(new CustomEvent('ibexa-inputs:added'));
45+
doc.body.dispatchEvent(
46+
new CustomEvent('ibexa-fieldtype-matrix:added-column', {
47+
detail: {
48+
columnNode: node.querySelector(`${SELECTOR_COLUMN}:last-of-type`),
49+
},
50+
}),
51+
);
4252
};
4353
const removeItems = (event) => {
4454
const settingsNode = event.target.closest(SELECTOR_SETTINGS_COLUMNS);
@@ -66,6 +76,7 @@
6676
});
6777

6878
node.closest('.ibexa-table').dispatchEvent(new CustomEvent('ibexa-refresh-main-table-checkbox'));
79+
validateColumnsNumber(settingsNode);
6980
}, 0);
7081

7182
initColumns(settingsNode);
@@ -91,6 +102,12 @@
91102

92103
initColumns(container);
93104
};
105+
const validateColumnsNumber = (settingsNode) => {
106+
const columns = settingsNode.querySelectorAll(SELECTOR_COLUMN);
107+
const errorNode = settingsNode.querySelector(ERROR_NODE_SELECTOR);
108+
109+
errorNode.toggleAttribute('hidden', columns.length > 0);
110+
};
94111

95112
doc.querySelectorAll(SELECTOR_SETTINGS_COLUMNS).forEach((container) => {
96113
initComponent(container);

src/bundle/Resources/views/themes/admin/matrix_fieldtype/field_types.html.twig

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
body_row_cols: [
2222
{ has_checkbox: true, content: col_checkbox, raw: true },
2323
{ content: form_widget(form.columns.vars.prototype.name), raw: true },
24-
{ content: form_widget(form.columns.vars.prototype.identifier), raw: true },
24+
{ content: form_widget(form.columns.vars.prototype.identifier, {
25+
attr: {
26+
class: 'ibexa-input--matrix-column-identifier',
27+
required: true,
28+
}
29+
}), raw: true },
2530
],
2631
class: 'ibexa-matrix-settings__column',
2732
} %}
@@ -40,7 +45,12 @@
4045
cols: [
4146
{ has_checkbox: true, content: col_checkbox, raw: true },
4247
{ content: form_widget(column.name), raw: true },
43-
{ content: form_widget(column.identifier), raw: true },
48+
{ content: form_widget(column.identifier, {
49+
attr: {
50+
class: 'ibexa-input--matrix-column-identifier',
51+
required: true
52+
}
53+
}), raw: true },
4454
],
4555
class: 'ibexa-matrix-settings__column',
4656
}]) %}
@@ -82,5 +92,14 @@
8292
{% endembed %}
8393
{% endblock %}
8494
{% endembed %}
95+
96+
<div class="ibexa-form-error" hidden>
97+
<em class="ibexa-form-error__row">
98+
<svg class="ibexa-icon ibexa-icon--small ibexa-form-error__icon">
99+
<use xlink:href="{{ ibexa_icon_path('notice') }}"></use>
100+
</svg>
101+
At least one column is required for matrix field.
102+
</em>
103+
</div>
85104
</div>
86105
{% endblock %}

0 commit comments

Comments
 (0)