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-5255: Move required validation method to helpers #728

Merged
merged 6 commits into from
Mar 14, 2023
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
1 change: 1 addition & 0 deletions src/bundle/Resources/encore/ibexa.js.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const layout = [
path.resolve(__dirname, '../public/js/scripts/helpers/pagination.helper.js'),
path.resolve(__dirname, '../public/js/scripts/helpers/object.instances.js'),
path.resolve(__dirname, '../public/js/scripts/helpers/middle.ellipsis.js'),
path.resolve(__dirname, '../public/js/scripts/helpers/form.validation.helper.js'),
path.resolve(__dirname, '../public/js/scripts/helpers/form.error.helper.js'),
path.resolve(__dirname, '../public/js/scripts/admin.format.date.js'),
path.resolve(__dirname, '../public/js/scripts/core/draggable.js'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@
if (isInputEmpty) {
const fieldName = labelNode.innerHTML;
const errorMessage = ibexa.errors.emptyField.replace('{fieldName}', fieldName);
const formattedError = ibexa.helpers.formError.formatLine(errorMessage);
const formattedError = ibexa.helpers.formValidation.formatErrorLine(errorMessage);

errorNode.append(formattedError);
}
Expand Down
10 changes: 10 additions & 0 deletions src/bundle/Resources/public/js/scripts/core/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
this.recreateOptions();
}
});
this.sourceInvalidObserver = new MutationObserver((mutationsList) => {
const isInvalid = mutationsList[0].target.classList.contains('is-invalid');

this.container.classList.toggle('is-invalid', isInvalid);
});
this.currentSelectedValue = this.sourceInput.value;

this.createSelectedItem = this.createSelectedItem.bind(this);
Expand Down Expand Up @@ -243,6 +248,7 @@
}

option.remove();
this.currentSelectedValue = null;

this.fitItems();
this.fireValueChangedEvent();
Expand Down Expand Up @@ -516,6 +522,10 @@
this.sourceOptionsObserver.observe(this.sourceInput, {
childList: true,
});
this.sourceInvalidObserver.observe(this.sourceInput, {
attributes: true,
attributeFilter: ['class'],
});

const selectedItems = this.container.querySelectorAll(
'.ibexa-dropdown__selected-item:not(.ibexa-dropdown__selected-overflow-number):not(.ibexa-dropdown__selected-placeholder)',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
* @memberof BaseFieldValidator
*/
createErrorNode(message) {
return ibexa.helpers.formError.formatLine(message);
return ibexa.helpers.formValidation.formatErrorLine(message);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
(function (global, doc, ibexa) {
const formatLine = (errorMessage) => {
const errorIcon = `<svg class="ibexa-icon ibexa-icon--small ibexa-form-error__icon">
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('warning-triangle')}"></use>
</svg>`;
const container = document.createElement('em');
const errorMessageNode = document.createTextNode(errorMessage);

container.classList.add('ibexa-form-error__row');
container.insertAdjacentHTML('beforeend', errorIcon);
container.append(errorMessageNode);

return container;
};

// @deprecated, will be removed in 5.0
dew326 marked this conversation as resolved.
Show resolved Hide resolved
ibexa.addConfig('helpers.formError', {
formatLine,
formatLine: (...args) => {
console.warn(
'helpers.formError.formatLine method is deprecated and will be removed in 5.0, please use helpers.formValidation.formatErrorLine instead.',
);

return ibexa.helpers.formValidation.formatErrorLine(...args);
},
});
})(window, window.document, window.ibexa);
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(function (global, doc, ibexa, Translator) {
const formatErrorLine = (errorMessage) => {
const errorIcon = `<svg class="ibexa-icon ibexa-icon--small ibexa-form-error__icon">
<use xlink:href="${ibexa.helpers.icon.getIconPath('warning-triangle')}"></use>
</svg>`;
const container = document.createElement('em');
const errorMessageNode = document.createTextNode(errorMessage);

container.classList.add('ibexa-form-error__row');
container.insertAdjacentHTML('beforeend', errorIcon);
container.append(errorMessageNode);

return container;
};
const checkIsEmpty = (field) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I dont like this func name

let errorMessage = '';
const input = field.querySelector('.ibexa-input');
const label = field.querySelector('.ibexa-label');

if (label) {
const fieldName = label.innerText;

errorMessage = Translator.trans(/*@Desc("%fieldName% Field is required")*/ 'error.required.field', { fieldName }, 'forms');
} else {
errorMessage = Translator.trans(/*@Desc("This value should not be blank")*/ 'error.required.field_not_blank', {}, 'forms');
}

return {
isValid: input.value,
errorMessage,
};
};
const validateIsEmptyField = (field) => {
const input = field.querySelector('.ibexa-input');
const errorWrapper = field.querySelector('.ibexa-form-error');
const validatorOutput = checkIsEmpty(field);
const { isValid, errorMessage } = validatorOutput;

input.classList.toggle('is-invalid', !isValid);
errorWrapper.innerText = '';

if (!isValid) {
errorWrapper.append(formatErrorLine(errorMessage));
}

return validatorOutput;
};
ibexa.addConfig('helpers.formValidation', {
formatErrorLine,
validateIsEmptyField,
});
})(window, window.document, window.ibexa, window.Translator);
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @returns {HTMLElement}
*/
const createErrorNode = (message) => {
return ibexa.helpers.formError.formatLine(message);
return ibexa.helpers.formValidation.formatErrorLine(message);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Resources/public/scss/_forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ form:not(.form-inline) {
color: $ibexa-color-danger;
line-height: calculateRem(18px);
box-sizing: border-box;
margin-top: calculateRem(4px);

&:not(& > &) {
flex-direction: column;
Expand All @@ -85,6 +84,7 @@ form:not(.form-inline) {
display: flex;
align-items: center;
font-style: normal;
margin-top: calculateRem(4px);
}

&__icon {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

.ibexa-form-error {
min-height: calculateRem(18px);
margin: calculateRem(4px) 0;
flex-direction: column;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
is_selector: attr.is_selector|default(false),
is_dynamic: attr.is_dynamic|default(false),
has_select_all_toggler: attr.has_select_all_toggler|default(false),
placeholder: attr.placeholder|default(placeholder),
} %}
{% endif %}
{%- endblock choice_widget -%}