Skip to content

Commit ea0bc2a

Browse files
committed
Add validation of the target file name in the "Save As" tab.
1 parent 1f3563b commit ea0bc2a

3 files changed

Lines changed: 33 additions & 14 deletions

File tree

htdocs/js/PGProblemEditor/pgproblemeditor.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -382,37 +382,55 @@
382382
}
383383
});
384384

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

406425
document.forms.editor?.addEventListener('submit', (e) => {
407426
if (actionSaveAs && actionSaveAs.classList.contains('active')) {
408-
if (saveToTargetSetRadio?.checked && !targetSetSelect?.value) {
409-
e.preventDefault();
410-
targetSetSelect.classList.add('is-invalid');
411-
targetSetSelect.setCustomValidity(targetSetSelect.dataset.errorMessage ?? 'Please select a set.');
412-
targetSetSelect.reportValidity();
413-
return;
427+
let report = true;
428+
for (const validationData of [
429+
[saveAsTargetFile, saveAsTargetFile?.value === '', 'Please enter a filename.'],
430+
[targetSetSelect, saveToTargetSetRadio?.checked && !targetSetSelect?.value, 'Please select a set.']
431+
]) {
432+
if (!validateInput(e, ...validationData, report)) report = false;
414433
}
415-
removeSetSelectErrors();
416434
}
417435
});
418436

templates/ContentGenerator/Instructor/PGProblemEditor.html.ep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<%= $fileInfo->() %>
7777
%
7878
<%= form_for current_route, method => 'POST', id => 'editor', name => 'editor',
79-
enctype => 'application/x-www-form-urlencoded', class => 'col-12', begin =%>
79+
enctype => 'application/x-www-form-urlencoded', class => 'col-12', novalidate => undef, begin =%>
8080
<%= $c->hidden_authen_fields =%>
8181
<%= hidden_field file_type => $c->{file_type} =%>
8282
<%= hidden_field courseID => $c->{courseID} =%>

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)