Skip to content

Commit afe7268

Browse files
update document name validation
1 parent ac6d95c commit afe7268

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

lib/workspaces/controllers/eg002AddDocumentToWorkspace.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { addDocumentToWorkspace } = require('../../workspaces/examples/addDocumen
99
const validator = require('validator');
1010
const { getExampleByNumber } = require('../../manifestService');
1111
const dsConfig = require('../../../config/index').config;
12-
const { API_TYPES } = require('../../utils');
12+
const { formatString, API_TYPES } = require('../../utils');
1313

1414
const eg002AddDocumentToWorkspace = exports;
1515
const exampleNumber = 2;
@@ -92,6 +92,7 @@ eg002AddDocumentToWorkspace.getController = async (req, res) => {
9292
eg: eg,
9393
csrfToken: req.csrfToken(),
9494
workspaceIdOk: !!req.session.workspaceId,
95+
documentFolder: formatString(res.locals.manifest.SupportingTexts.HelpingTexts.SelectPDFFileFromFolder, demoDocsPath),
9596
example: example,
9697
sourceFile: sourceFile,
9798
sourceUrl: dsConfig.githubExampleUrl + 'workspaces/examples/' + sourceFile,

public/assets/css.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pre.json-display {
109109
opacity: unset;
110110
}
111111

112-
form .form-group #error {
112+
form .form-group .validation-error {
113113
color: red !important;
114114
}
115115

views/pages/workspaces-examples/eg002AddDocumentToWorkspace.ejs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<%- include("../../partials/exampleInfo") %>
44

55
<% if (workspaceIdOk) { %>
6-
<form class="eg" action="" method="post" data-busy="form">
6+
<form id="uploadForm" class="eg" action="" method="post" data-busy="form">
77
<% if(example.Forms && example.Forms[0].FormName) { %>
88
<%- example.Forms[0].FormName %>
99
<% } %>
@@ -12,37 +12,55 @@
1212
<label for="documentName"><%= example.Forms[0].Inputs[0].InputName %></label>
1313
<input type="text" class="form-control" id="documentName" placeholder="<%= example.Forms[0].Inputs[0].InputPlaceholder %>"
1414
name="documentName" required>
15+
<small id="nameError" class="form-text text-muted validation-error"></small>
16+
<small id="emailHelp" class="form-text text-muted"><%= locals.manifest.SupportingTexts.HelpingTexts.SpecifyNameWithExtension %></small>
1517
</div>
1618
1719
<div class="form-group">
1820
<label for="documentPath"><%= example.Forms[0].Inputs[1].InputName %></label>
1921
<input type="file" accept=".pdf" class="form-control" id="documentPath" placeholder="<%= example.Forms[0].Inputs[1].InputPlaceholder %>"
2022
name="documentPath" required>
21-
<small id="error" class="form-text text-muted"></small>
23+
<small id="pathError" class="form-text text-muted validation-error"></small>
24+
<small id="emailHelp" class="form-text text-muted"><%= documentFolder %></small>
2225
</div>
2326
2427
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
2528
<%- include("../../partials/submitButton") %>
2629
</form>
2730
2831
<script>
29-
const fileInput = document.getElementById('documentPath');
30-
const errorEl = document.getElementById('error');
32+
const form = document.getElementById('uploadForm');
33+
form.addEventListener('submit', function (event) {
34+
const docNameInput = document.getElementById('documentName');
35+
const fileInput = document.getElementById('documentPath');
36+
const nameErrorEl = document.getElementById('nameError');
37+
const pathErrorEl = document.getElementById('pathError');
3138
32-
fileInput.addEventListener('change', (e) => {
39+
const documentName = docNameInput.value.trim();
3340
const file = fileInput.files[0];
3441
42+
// Validate documentName ends with .pdf
43+
if (!documentName.toLowerCase().endsWith(".pdf")) {
44+
nameErrorEl.textContent = 'Document name must contain .pdf extension';
45+
event.preventDefault();
46+
event.stopImmediatePropagation();
47+
} else {
48+
nameErrorEl.textContent = ''; // clear any previous errors
49+
}
50+
3551
// Check extension
3652
const isPdf = file.name.toLowerCase().endsWith('.pdf');
3753
const isPdfMime = file.type === 'application/pdf';
3854
3955
if (!isPdf || !isPdfMime) {
4056
fileInput.value = ''; // reset file input
41-
errorEl.textContent = 'Invalid file type. Please upload a PDF file.';
57+
pathErrorEl.textContent = 'Invalid file type. Please upload a PDF file.';
58+
event.preventDefault();
59+
event.stopImmediatePropagation();
4260
} else {
43-
errorEl.textContent = ''; // clear any previous errors
61+
pathErrorEl.textContent = ''; // clear any previous errors
4462
}
45-
});
63+
}, true);
4664
</script>
4765
<% } else { %>
4866
<%- formatString(example.RedirectsToOtherCodeExamples[0].RedirectText, formatString('href="work00{0}"', example.RedirectsToOtherCodeExamples[0].CodeExampleToRedirectTo)) %>

0 commit comments

Comments
 (0)