-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
74 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/bundle/Resources/public/js/scripts/fieldType/validator/richtext-validator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const { ibexa } = window; | ||
|
||
class RichTextValidator extends ibexa.BaseFieldValidator { | ||
constructor({ richtextEditor, selectorField, labelSelector, ...config }) { | ||
super(config); | ||
|
||
this.richtextEditor = richtextEditor; | ||
this.selectorField = selectorField; | ||
this.labelSelector = labelSelector; | ||
} | ||
|
||
/** | ||
* Validates the input | ||
* | ||
* @method validateInput | ||
* @param {Event} event | ||
* @returns {Object} | ||
* @memberof RichTextValidator | ||
*/ | ||
validateInput(event) { | ||
const fieldContainer = event.currentTarget.closest(this.selectorField); | ||
const isRequired = fieldContainer.classList.contains('ibexa-field-edit--required'); | ||
const label = fieldContainer.querySelector(this.labelSelector).innerHTML; | ||
const isEmpty = !this.richtextEditor.getData().length; | ||
const isError = isRequired && isEmpty; | ||
const result = { isError }; | ||
|
||
if (isError) { | ||
result.errorMessage = ibexa.errors.emptyField.replace('{fieldName}', label); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
const initValidator = (container, selectorField, selectorErrorNone, selectorInput, labelSelector, richtextEditor) => { | ||
const validator = new RichTextValidator({ | ||
classInvalid: 'is-invalid', | ||
fieldContainer: container.closest(selectorField), | ||
eventsMap: [ | ||
{ | ||
selector: '.ibexa-data-source__input.ibexa-input--textarea', | ||
eventName: 'input', | ||
callback: 'validateInput', | ||
errorNodeSelectors: [selectorErrorNone], | ||
}, | ||
{ | ||
selector: selectorInput, | ||
eventName: 'blur', | ||
callback: 'validateInput', | ||
errorNodeSelectors: [selectorErrorNone], | ||
}, | ||
], | ||
richtextEditor, | ||
selectorField, | ||
selectorInput, | ||
labelSelector, | ||
}); | ||
|
||
validator.init(); | ||
|
||
return validator; | ||
}; | ||
|
||
export default initValidator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters