Skip to content

Commit 72c5141

Browse files
committed
File Uploads: Added basic validation response formatting
Tested via app-level validation file limit, and then also with nginx file post limit. For #4996
1 parent 5651d2c commit 72c5141

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

resources/js/components/dropzone.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,7 @@ export class Dropzone extends Component {
181181
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
182182
upload.markSuccess(component.successMessage);
183183
} else if (this.readyState === XMLHttpRequest.DONE && this.status >= 400) {
184-
const content = this.responseText;
185-
const data = content.startsWith('{') ? JSON.parse(content) : {message: content};
186-
const message = data?.message || data?.error || content;
187-
upload.markError(message);
184+
upload.markError(window.$http.formatErrorResponseText(this.responseText));
188185
}
189186
},
190187
});

resources/js/services/http.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,32 @@ async function performDelete(url, data = null) {
207207
}
208208

209209
export {performDelete as delete};
210+
211+
/**
212+
* Parse the response text for an error response to a user
213+
* presentable string. Handles a range of errors responses including
214+
* validation responses & server response text.
215+
* @param {String} text
216+
* @returns {String}
217+
*/
218+
export function formatErrorResponseText(text) {
219+
const data = text.startsWith('{') ? JSON.parse(text) : {message: text};
220+
if (!data) {
221+
return text;
222+
}
223+
224+
if (data.message || data.error) {
225+
return data.message || data.error;
226+
}
227+
228+
const values = Object.values(data);
229+
const isValidation = values.every(val => {
230+
return Array.isArray(val) || val.every(x => typeof x === 'string');
231+
});
232+
233+
if (isValidation) {
234+
return values.flat().join(' ');
235+
}
236+
237+
return text;
238+
}

resources/sass/_components.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ body.flexbox-support #entity-selector-wrap .popup-body .form-group {
321321
background-color: var(--color-primary);
322322
transition: width ease-in-out 240ms;
323323
}
324+
.dropzone-file-item-label {
325+
line-height: 1.2;
326+
margin-bottom: .2rem;
327+
}
324328
.dropzone-file-item-label,
325329
.dropzone-file-item-status {
326330
align-items: center;

0 commit comments

Comments
 (0)