Skip to content

Commit

Permalink
IBX-9274: Added URL validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasOsti committed Jan 14, 2025
1 parent 9c88bf4 commit 79e777a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/bundle/Resources/public/js/scripts/fieldType/ezurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@

class EzUrlValidator extends ibexa.BaseFieldValidator {
validateUrl(event) {
const result = {
isError: false,
errorMessage: null,
};
const input = event.currentTarget;
const urlValue = input.value.trim();
const isRequired = input.required;
const isEmpty = !input.value.trim();
const isError = isEmpty && isRequired;
const isEmpty = !urlValue;
const label = input.closest(SELECTOR_FIELD_LINK).querySelector(SELECTOR_LABEL).innerHTML;
const result = { isError };

if (isRequired && isEmpty) {
result.isError = true;
result.errorMessage = ibexa.errors.emptyField.replace('{fieldName}', label);
}

if (!isEmpty) {
try {
new URL(urlValue);
} catch (error) {
result.isError = true;
result.errorMessage = ibexa.errors.invalidUrl;
}
}

return result;
}
}
Expand Down

0 comments on commit 79e777a

Please sign in to comment.