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

IBX-2360: Validation content type create/edit #334

Merged
merged 5 commits into from
Feb 28, 2022
Merged
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
95 changes: 79 additions & 16 deletions src/bundle/Resources/public/js/scripts/admin.contenttype.edit.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
(function(global, doc, ibexa, Routing, Translator) {
const TIMEOUT_REMOVE_PLACEHOLDERS = 1500;
const SELECTOR_INPUTS_TO_VALIDATE = '.ibexa-input[required]:not([disabled]):not([hidden])';
let targetContainer = null;
let sourceContainer = null;
let currentDraggedItem = null;
let draggedItemPosition = null;
let isEditFormValid = false;
const editForm = doc.querySelector('.ibexa-content-type-edit-form');
let inputsToValidate = editForm.querySelectorAll(SELECTOR_INPUTS_TO_VALIDATE);
const draggableGroups = [];
const token = doc.querySelector('meta[name="CSRF-Token"]').content;
const siteaccess = doc.querySelector('meta[name="SiteAccess"]').content;
const sectionsNode = doc.querySelector('.ibexa-content-type-edit__sections');
const filterFieldInput = doc.querySelector('.ibexa-available-field-types__sidebar-filter');
const popupMenuElement = sectionsNode.querySelector('.ibexa-popup-menu');
const addGroupTriggerBtn = sectionsNode.querySelector('.ibexa-content-type-edit__add-field-definitions-group-btn');
const noFieldsAddedError = Translator.trans(
/*@Desc("You have to add at least one field definition")*/ 'content_type.edit.error.no_added_fields_definition',
{},
'content_type',
);
const endpoints = {
add: {
actionName: 'add_field_definition',
Expand Down Expand Up @@ -80,7 +89,9 @@

const fieldGroupInput = fieldNode.querySelector('.ibexa-input--field-group');
const removeFieldsBtn = fieldNode.querySelectorAll('.ibexa-collapse__extra-action-button--remove-field-definitions');
const fieldInputsToValidate = fieldNode.querySelectorAll(SELECTOR_INPUTS_TO_VALIDATE);

fieldInputsToValidate.forEach(attachValidateEvents);
removeDragPlaceholders();
fieldGroupInput.value = fieldsGroupId;
targetContainer.insertBefore(fieldNode, targetPlace);
Expand Down Expand Up @@ -132,8 +143,6 @@
});
};
const afterChangeGroup = () => {
const submitBtn = doc.querySelector('.ibexa-content-type-edit__publish-content-type');
const fieldsDefinitionCount = doc.querySelectorAll('.ibexa-collapse--field-definition').length;
const groups = doc.querySelectorAll('.ibexa-collapse--field-definitions-group');
const itemsAction = doc.querySelectorAll('.ibexa-content-type-edit__add-field-definitions-group .ibexa-popup-menu__item-content');

Expand All @@ -158,8 +167,6 @@
doc.querySelectorAll('.ibexa-collapse--field-definition').forEach((fieldDefinition, index) => {
fieldDefinition.querySelector('.ibexa-input--position').value = index;
});

submitBtn.toggleAttribute('disabled', !fieldsDefinitionCount);
};
const addField = () => {
if (!sourceContainer.classList.contains('ibexa-available-field-types__list')) {
Expand Down Expand Up @@ -253,7 +260,58 @@
})
.catch(ibexa.helpers.notification.showErrorNotification);
};
const validateInput = (input) => {
const isInputEmpty = !input.value;
const field = input.closest('.form-group');
const labelNode = field.querySelector('.ibexa-label');
const errorNode = field.querySelector('.ibexa-form-error');

input.classList.toggle('is-invalid', isInputEmpty);

if (errorNode) {
const fieldName = labelNode.innerHTML;
const errorMessage = ibexa.errors.emptyField.replace('{fieldName}', fieldName);

errorNode.innerHTML = isInputEmpty ? errorMessage : '';
}

isEditFormValid = isEditFormValid && !isInputEmpty;
};
const validateForm = () => {
const fieldDefinitionsStatuses = {};

isEditFormValid = true;
inputsToValidate = editForm.querySelectorAll(SELECTOR_INPUTS_TO_VALIDATE);

inputsToValidate.forEach((input) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: could this be iterated field definition by field definition without fieldDefinitionsStatuses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved in private conversation

const fieldDefinition = input.closest('.ibexa-collapse--field-definition');

if (fieldDefinition) {
const { fieldDefinitionIdentifier } = fieldDefinition.dataset;
const isInputEmpty = !input.value;

if (!fieldDefinitionsStatuses[fieldDefinitionIdentifier]) {
fieldDefinitionsStatuses[fieldDefinitionIdentifier] = [];
}

fieldDefinitionsStatuses[fieldDefinitionIdentifier].push(isInputEmpty);
}

validateInput(input);
});

Object.entries(fieldDefinitionsStatuses).forEach(([fieldDefinitionIdentifier, inputsStatus]) => {
const isFieldDefinitionValid = inputsStatus.every((hasError) => !hasError);
const fieldDefinitionNode = doc.querySelector(`[data-field-definition-identifier="${fieldDefinitionIdentifier}"]`);

fieldDefinitionNode.classList.toggle('is-invalid', !isFieldDefinitionValid);
});
};
const attachValidateEvents = (input) => {
input.addEventListener('change', validateForm, false);
input.addEventListener('blur', validateForm, false);
input.addEventListener('input', validateForm, false);
};
class FieldDefinitionDraggable extends ibexa.core.Draggable {
onDrop(event) {
targetContainer = event.currentTarget;
Expand Down Expand Up @@ -368,20 +426,25 @@
draggableGroups.push(draggable);
});

doc.querySelector('.ibexa-btn--save-content-type').addEventListener(
'click',
() => {
if (doc.querySelectorAll('.ibexa-collapse--field-definition').length) {
return;
inputsToValidate.forEach(attachValidateEvents);

editForm.addEventListener(
'submit',
(event) => {
const fieldDefinitionsCount = doc.querySelectorAll('.ibexa-collapse--field-definition').length;

validateForm();

if (!fieldDefinitionsCount) {
isEditFormValid = false;
Comment on lines +438 to +439
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is you put this check here instead of validateForm, because you wanted to display a noFieldsAddedError message, but for me, it is not intuitive to have this checkout outside validateForm.
Can it be put there maybe? Or maybe it is intuitive for you? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved in private conversation

ibexa.helpers.notification.showErrorNotification(noFieldsAddedError);
}

ibexa.helpers.notification.showErrorNotification(
Translator.trans(
/*@Desc("You have to add at least one field definition")*/ 'content_type.edit.error.no_added_fields_definition',
{},
'content_type',
),
);
if (!isEditFormValid) {
event.preventDefault();

return;
}
},
false,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
{% endblock %}

{% block form %}
{{ form_start(form, { attr: { class: 'ibexa-form' } } ) }}
{{ form_start(form, {
attr: {
class: 'ibexa-content-type-edit-form ibexa-form',
novalidate: 'novalidate',
}
}) }}
<div
class="ibexa-content-type-edit__sections ibexa-anchor-navigation-sections container"
data-language-code="{{ language_code }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
{% endblock %}

{% block form %}
{{ form_start(form, { attr: { class: 'ibexa-content-type-edit ibexa-form' } }) }}
{{ form_start(form, {
attr: {
class: 'ibexa-content-type-edit-form ibexa-form',
novalidate: 'novalidate',
}
}) }}
{% set is_translation = form.vars.data.languageCode != content_type.mainLanguageCode %}
{% set is_draggable = is_translation == false %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
{{ ibexa_render_field_definition_edit(value, {
'languageCode': language_code,
'form': field_definition,
'group_class': value.group_class|default(''),
'group_class': value.group_class|default('') ~ ' form-group',
'is_translation': is_translation ?? false,
}) }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
{{ ibexa_render_field_definition_edit(value, {
'languageCode': language_code,
'form': form,
'group_class': value.group_class|default(''),
'group_class': value.group_class|default('') ~ ' form-group',
'is_translation': is_translation,
}) }}

Expand Down
1 change: 0 additions & 1 deletion src/lib/Form/Type/ContentType/ContentTypeUpdateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('removeDraft', SubmitType::class, ['label' => /** @Desc("Cancel") */ 'content_type.remove_draft', 'validation_groups' => false])
->add('publishContentType', SubmitType::class, [
'label' => /** @Desc("OK") */ 'content_type.publish',
'disabled' => !$hasFieldDefinition,
])
;
}
Expand Down