Skip to content

Commit ab6e2e2

Browse files
committed
Add validation of the target file name in the "Save As" tab.
1 parent 76af25a commit ab6e2e2

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

htdocs/js/PGProblemEditor/pgproblemeditor.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -380,37 +380,55 @@
380380
}
381381
});
382382

383+
const removeValidationErrors = (input) => {
384+
input?.classList.remove('is-invalid');
385+
input.setCustomValidity('');
386+
};
387+
388+
const validateInput = (e, input, isInvalid, defaultMessage, report) => {
389+
if (isInvalid) {
390+
e.preventDefault();
391+
input.classList.add('is-invalid');
392+
input.setCustomValidity(input.dataset.errorMessage ?? defaultMessage);
393+
if (report) input.reportValidity();
394+
return false;
395+
}
396+
removeValidationErrors(input);
397+
return true;
398+
};
399+
400+
// Validation of the target file for the save as tab.
401+
const saveAsTargetFile = document.getElementsByName('action.save_as.target_file')?.[0];
402+
saveAsTargetFile.addEventListener('keyup', () => {
403+
if (saveAsTargetFile.value) removeValidationErrors(saveAsTargetFile);
404+
});
405+
383406
// Validation of the target set for the save as tab.
384407
const saveAsSaveModeRadios = document.getElementsByName('action.save_as.saveMode');
385408
const saveToTargetSetRadio = Array.from(saveAsSaveModeRadios).find(
386409
(r) => r.id === 'action_save_as_saveMode_new_problem_id' || r.id === 'action_save_as_saveMode_set_header_id'
387410
);
388411
const targetSetSelect = document.getElementsByName('action.save_as.targetSet')?.[0];
389412
const actionSaveAs = document.getElementById('save_as');
390-
const removeSetSelectErrors = () => {
391-
targetSetSelect?.classList.remove('is-invalid');
392-
targetSetSelect?.setCustomValidity('');
393-
};
394413
for (const radio of saveAsSaveModeRadios) {
395-
radio.addEventListener('change', removeSetSelectErrors);
414+
radio.addEventListener('change', () => removeValidationErrors(targetSetSelect));
396415
}
397416
const saveToTargetSetSelected = () => {
398417
saveToTargetSetRadio.checked = true;
399-
if (targetSetSelect?.value) removeSetSelectErrors();
418+
if (targetSetSelect?.value) removeValidationErrors(targetSetSelect);
400419
};
401420
targetSetSelect?.addEventListener('change', saveToTargetSetSelected);
402421
targetSetSelect?.addEventListener('focusin', saveToTargetSetSelected);
403422

404423
document.forms.editor?.addEventListener('submit', (e) => {
405424
if (actionSaveAs && actionSaveAs.classList.contains('active')) {
406-
if (saveToTargetSetRadio?.checked && !targetSetSelect?.value) {
407-
e.preventDefault();
408-
targetSetSelect.classList.add('is-invalid');
409-
targetSetSelect.setCustomValidity(targetSetSelect.dataset.errorMessage ?? 'Please select a set.');
410-
targetSetSelect.reportValidity();
411-
return;
425+
let report = true;
426+
for (const validationData of [
427+
[saveAsTargetFile, saveAsTargetFile?.value === '', 'Please enter a filename.'],
428+
[targetSetSelect, saveToTargetSetRadio?.checked && !targetSetSelect?.value, 'Please select a set.']
429+
]) {
430+
if (!validateInput(e, ...validationData, report)) report = false;
412431
}
413-
removeSetSelectErrors();
414432
}
415433
});
416434

templates/ContentGenerator/Instructor/PGProblemEditor/save_as_form.html.ep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<%= text_field 'action.save_as.target_file' => $shortFilePath,
3535
id => 'action_save_as_target_file_id', class => 'form-control form-control-sm',
3636
size => 60, dir => 'ltr',
37+
data => { error_message => maketext('Please enter a filename.') },
3738
# Don't allow changing the file name for course info files.
3839
# The filename needs to be what is set in the course environment.
3940
$c->{file_type} eq 'course_info' ? (readonly => undef) : () =%>

0 commit comments

Comments
 (0)