Skip to content

Commit 8cc290b

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

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
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/public/scss/_matrix-settings.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
.ibexa-matrix-settings {
2+
&__columns {
3+
margin-top: calculateRem(16px)
4+
}
5+
26
&__column {
37
.form-check {
48
padding-left: 0;

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@
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+
}
28+
}), raw: true },
2529
],
2630
class: 'ibexa-matrix-settings__column',
2731
} %}
2832
</script>
33+
2934
{{ form_label(form.columns, null, { 'label_attr': { 'class': 'ibexa-label' } }) }}
3035
{{ form_errors(form.columns) }}
3136
{% set body_rows = [] %}
@@ -40,7 +45,11 @@
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+
}
52+
}), raw: true },
4453
],
4554
class: 'ibexa-matrix-settings__column',
4655
}]) %}
@@ -82,5 +91,14 @@
8291
{% endembed %}
8392
{% endblock %}
8493
{% endembed %}
94+
95+
<div class="ibexa-form-error" hidden>
96+
<em class="ibexa-form-error__row">
97+
<svg class="ibexa-icon ibexa-icon--small ibexa-form-error__icon">
98+
<use xlink:href="{{ ibexa_icon_path('notice') }}"></use>
99+
</svg>
100+
At least one column is required for matrix field.
101+
</em>
102+
</div>
85103
</div>
86104
{% endblock %}

src/lib/FieldType/Mapper/MatrixFormMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field
4343
])
4444
->add('columns', CollectionType::class, [
4545
'entry_type' => ColumnType::class,
46-
'entry_options' => ['required' => false],
46+
'entry_options' => ['required' => true],
4747
'allow_add' => true,
4848
'allow_delete' => true,
4949
'delete_empty' => false,
5050
'prototype' => true,
5151
'prototype_name' => '__number__',
52-
'required' => false,
52+
'required' => true,
5353
'property_path' => 'fieldSettings[columns]',
5454
'label' => false,
5555
'translation_domain' => 'ibexa_matrix_fieldtype',

src/lib/Form/Type/ColumnType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
2020
$builder
2121
->add('name', TextType::class, [
2222
'label' => /** @Desc("Name") */ 'fieldtype.column.name',
23+
'required' => false,
2324
])
2425
->add('identifier', TextType::class, [
2526
'label' => /** @Desc("Identifier") */ 'fieldtype.column.identifier',
27+
'required' => true,
2628
]);
2729
}
2830

0 commit comments

Comments
 (0)