Skip to content

Commit cae8c18

Browse files
albozekbarbaragr
authored andcommitted
Isbn field required validation
1 parent c244450 commit cae8c18

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
(function (global, doc, ibexa) {
2+
const SELECTOR_FIELD = '.ibexa-field-edit--ezisbn';
3+
const SELECTOR_SOURCE_INPUT = '.ibexa-data-source__input';
4+
const SELECTOR_LABEL = '.ibexa-field-edit__label';
5+
6+
class IbexaIsbnValidator extends ibexa.BaseFieldValidator {
7+
/**
8+
* Validates the input
9+
*
10+
* @method validateInput
11+
* @param {Event} event
12+
* @returns {Object}
13+
* @memberof IbexaIsbnValidator
14+
*/
15+
validateInput(event) {
16+
const isError = event.target.required && !event.target.value.trim();
17+
const label = event.target.closest(SELECTOR_FIELD).querySelector(SELECTOR_LABEL).innerText;
18+
const errorMessage = ibexa.errors.emptyField.replace('{fieldName}', label);
19+
20+
return {
21+
isError,
22+
errorMessage,
23+
};
24+
}
25+
}
26+
27+
const validator = new IbexaIsbnValidator({
28+
classInvalid: 'is-invalid',
29+
fieldSelector: SELECTOR_FIELD,
30+
eventsMap: [
31+
{
32+
selector: `${SELECTOR_FIELD} input`,
33+
eventName: 'blur',
34+
callback: 'validateInput',
35+
errorNodeSelectors: ['.ibexa-form-error'],
36+
invalidStateSelectors: [SELECTOR_SOURCE_INPUT, SELECTOR_LABEL],
37+
},
38+
],
39+
});
40+
41+
validator.init();
42+
43+
ibexa.addConfig('fieldTypeValidators', [validator], true);
44+
})(window, window.document, window.ibexa);

0 commit comments

Comments
 (0)