From 79e777a458dd40e71f6ced20aed421c35ad4a717 Mon Sep 17 00:00:00 2001 From: Lukasz Ostafin Date: Tue, 14 Jan 2025 10:22:22 +0100 Subject: [PATCH] IBX-9274: Added URL validation --- .../public/js/scripts/fieldType/ezurl.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/bundle/Resources/public/js/scripts/fieldType/ezurl.js b/src/bundle/Resources/public/js/scripts/fieldType/ezurl.js index dab4cd47dd..1935bdd5a9 100644 --- a/src/bundle/Resources/public/js/scripts/fieldType/ezurl.js +++ b/src/bundle/Resources/public/js/scripts/fieldType/ezurl.js @@ -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; } }