-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check edit media form validity before allowing changes to be saved
This commit adds client-side JavaScript code that checks the edit-media form as the user is changing the form. It then performs a server-side check that the provided values are valid. If they are not valid, the client-side code updates the form to show the errors and disables the submit button. This stops the user from creating new media files that will fail the validation checks.
- Loading branch information
Showing
5 changed files
with
134 additions
and
1 deletion.
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
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
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,72 @@ | ||
/* globals console */ | ||
import $ from '/libs/jquery.js'; | ||
import { enableTooltips } from './tooltips.js'; | ||
|
||
async function checkFormValues() { | ||
const form = document.getElementById('edit_media'); | ||
if (form === null) { | ||
console.error('Failed to find edit media form'); | ||
return; | ||
} | ||
const url = $(form).data('validate-url'); | ||
if (!url) { | ||
console.error('Failed to find URL for form validation'); | ||
return; | ||
} | ||
const data = Object.fromEntries( | ||
$(form).serializeArray(). | ||
filter(item => item.name !== 'csrf_token'). | ||
map(item => [item.name, item.value,])); | ||
$('form').addClass('needs-validation').removeClass('was-validated'); | ||
const result = await fetch(url, { | ||
body: JSON.stringify(data), | ||
cache: 'no-cache', | ||
credentials: 'same-origin', | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
method: 'POST', | ||
mode: 'same-origin', | ||
}); | ||
if (!result.ok) { | ||
console.error(`Failed to check form validitity: ${ result.status }`); | ||
return; | ||
} | ||
const { errors } = await result.json(); | ||
$('#edit_media input').each((_index, elt) => { | ||
const $elt = $(elt); | ||
const name = $elt.attr('name'); | ||
const parent = $elt.parent(); | ||
const feedback = parent.find('.invalid-feedback'); | ||
const err = errors[name] || ''; | ||
if (err === '') { | ||
$elt.addClass('is-valid').removeClass('is-invalid'); | ||
} else { | ||
$elt.removeClass('is-valid').addClass('is-invalid'); | ||
} | ||
feedback.text(err); | ||
elt.setCustomValidity(err); | ||
}); | ||
$('form').removeClass('needs-validation').addClass('was-validated'); | ||
const submit = $('#edit_media button[type="submit"]'); | ||
if (form.checkValidity()) { | ||
submit.attr('disabled', false); | ||
} else { | ||
submit.attr('disabled', 'disabled'); | ||
} | ||
} | ||
|
||
function setupValidation() { | ||
$('#edit_media input').on('change', checkFormValues); | ||
|
||
$('#edit_media input').on('invalid', (ev) => { | ||
$(ev.target).removeClass('is-valid').addClass('is-invalid'); | ||
}); | ||
|
||
$('#edit_media input').on('valid', (ev) => { | ||
$(ev.target).addClass('is-valid').removeClass('is-invalid'); | ||
}); | ||
} | ||
|
||
enableTooltips(); | ||
setupValidation(); |
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
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