Skip to content

Commit

Permalink
Initial move away from Joomla core form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
billtomczak committed Jun 14, 2022
1 parent ae01986 commit 73bff34
Show file tree
Hide file tree
Showing 7 changed files with 1,720 additions and 208 deletions.
5 changes: 4 additions & 1 deletion src/media/css/frontend.css
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,14 @@ Example markup
}

.osdownloads-modal .error {
margin-bottom: 20px;
font-style: italic;
color: #d85030;
}

.osdownloads-modal label.error {
margin-bottom: 20px;
}

.osdownloads-modal label {
width: 100%;
}
Expand Down
110 changes: 20 additions & 90 deletions src/media/js/jquery.osdownloads.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,59 +174,11 @@
// Move the popup containers to the body
$popup.appendTo($('body'));

let isValidForm = function() {
let email = $fieldEmail.val().trim(),
emailRegex = /^([A-Za-z0-9_\-.+])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,25})$/,
hasError = false;

if (requireAgree) {
if ($fieldAgree.is(':checked')) {
$errorAgreeTerms.hide();

} else {
hasError = true;
$errorAgreeTerms.show();
}
}

switch (requireEmail) {
case 1:
// email required
if (email === '' || !emailRegex.test(email)) {
hasError = true;
$errorInvalidEmail.show();

} else {
$errorInvalidEmail.hide();
}
break;

case 2:
// email optional
if (email !== '' && !emailRegex.test(email)) {
hasError = true;
$errorInvalidEmail.show();

} else {
$errorInvalidEmail.hide();
}
break;
}

if (hasError) {
return false;
}

// Validate the form for custom fields before submitting
if ($form.length > 0) {
$form.prop('target', 'osdownloads-tmp-iframe-' + $form.prop('id'));

return document.formvalidator.isValid($form[0]);

} else {
return true;
$form.validate({
submitHandler: function(form) {
download();
}
};
});

let showPopup = function(selector) {
$(selector).reveal({
Expand All @@ -247,7 +199,7 @@
.addClass('osdownloads-modal');

$iframe = $('<iframe>').prop('name', 'osdownloads-tmp-iframe-' + $form.prop('id'));
$close = $('<a class="close-reveal-modal">&#215;</a>');
$close = $('<a class="close-reveal-modal">&#215;</a>');

$iframe.appendTo($container);
$close.appendTo($container);
Expand All @@ -273,47 +225,25 @@
event.preventDefault();
event.stopPropagation();

if (requireEmail || requireAgree) {
if (requireEmail !== 0) {
$groupEmail.show();

} else {
$groupEmail.hide();
}
$btnContinue.prop('href', $this.prop('href'));

if (requireAgree) {
$groupAgree.find('.agreement-article').prop('href', $this.data('agreement-article'));
$groupAgree.show();
showPopup('#' + popupElementId);

} else {
$groupAgree.hide();
$popup.on(
'reveal:close',
function requirementsRevealOnClose() {
// Clean fields
$fieldEmail.val('');
$fieldAgree.prop('checked', false);
}
);

$btnContinue.prop('href', $this.prop('href'));

showPopup('#' + popupElementId);

$popup.on(
'reveal:close',
function requirementsRevealOnClose() {
// Clean fields
$fieldEmail.val('');
$fieldAgree.prop('checked', false);
$('.osdownloads-modal .error').hide();
}
);

$btnContinue.off();
$btnContinue.on('click', function continueBtnOnClick(event) {
event.preventDefault();

if (isValidForm()) {
download();
}
});
} else {
download();
}
$btnContinue.off();
$btnContinue.on('click', function continueBtnOnClick(event) {
event.preventDefault();

$form.submit();
});
});

$this.data('osdownloads-loaded', 1);
Expand Down
9 changes: 1 addition & 8 deletions src/media/js/jquery.osdownloads.bundle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 20 additions & 90 deletions src/media/js/jquery.osdownloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,59 +47,11 @@
// Move the popup containers to the body
$popup.appendTo($('body'));

let isValidForm = function() {
let email = $fieldEmail.val().trim(),
emailRegex = /^([A-Za-z0-9_\-.+])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,25})$/,
hasError = false;

if (requireAgree) {
if ($fieldAgree.is(':checked')) {
$errorAgreeTerms.hide();

} else {
hasError = true;
$errorAgreeTerms.show();
}
}

switch (requireEmail) {
case 1:
// email required
if (email === '' || !emailRegex.test(email)) {
hasError = true;
$errorInvalidEmail.show();

} else {
$errorInvalidEmail.hide();
}
break;

case 2:
// email optional
if (email !== '' && !emailRegex.test(email)) {
hasError = true;
$errorInvalidEmail.show();

} else {
$errorInvalidEmail.hide();
}
break;
}

if (hasError) {
return false;
}

// Validate the form for custom fields before submitting
if ($form.length > 0) {
$form.prop('target', 'osdownloads-tmp-iframe-' + $form.prop('id'));

return document.formvalidator.isValid($form[0]);

} else {
return true;
$form.validate({
submitHandler: function(form) {
download();
}
};
});

let showPopup = function(selector) {
$(selector).reveal({
Expand All @@ -120,7 +72,7 @@
.addClass('osdownloads-modal');

$iframe = $('<iframe>').prop('name', 'osdownloads-tmp-iframe-' + $form.prop('id'));
$close = $('<a class="close-reveal-modal">&#215;</a>');
$close = $('<a class="close-reveal-modal">&#215;</a>');

$iframe.appendTo($container);
$close.appendTo($container);
Expand All @@ -146,47 +98,25 @@
event.preventDefault();
event.stopPropagation();

if (requireEmail || requireAgree) {
if (requireEmail !== 0) {
$groupEmail.show();

} else {
$groupEmail.hide();
}
$btnContinue.prop('href', $this.prop('href'));

if (requireAgree) {
$groupAgree.find('.agreement-article').prop('href', $this.data('agreement-article'));
$groupAgree.show();
showPopup('#' + popupElementId);

} else {
$groupAgree.hide();
$popup.on(
'reveal:close',
function requirementsRevealOnClose() {
// Clean fields
$fieldEmail.val('');
$fieldAgree.prop('checked', false);
}
);

$btnContinue.prop('href', $this.prop('href'));

showPopup('#' + popupElementId);

$popup.on(
'reveal:close',
function requirementsRevealOnClose() {
// Clean fields
$fieldEmail.val('');
$fieldAgree.prop('checked', false);
$('.osdownloads-modal .error').hide();
}
);

$btnContinue.off();
$btnContinue.on('click', function continueBtnOnClick(event) {
event.preventDefault();

if (isValidForm()) {
download();
}
});
} else {
download();
}
$btnContinue.off();
$btnContinue.on('click', function continueBtnOnClick(event) {
event.preventDefault();

$form.submit();
});
});

$this.data('osdownloads-loaded', 1);
Expand Down
Loading

0 comments on commit 73bff34

Please sign in to comment.