kopia lustrzana https://github.com/wagtail/wagtail
Move validation into the base chooser modal implementation
rodzic
49720cc08f
commit
0b752a81ac
|
@ -2,43 +2,20 @@ import $ from 'jquery';
|
||||||
import { initTabs } from '../../includes/tabs';
|
import { initTabs } from '../../includes/tabs';
|
||||||
import {
|
import {
|
||||||
submitCreationForm,
|
submitCreationForm,
|
||||||
|
validateCreationForm,
|
||||||
initPrefillTitleFromFilename,
|
initPrefillTitleFromFilename,
|
||||||
SearchController,
|
SearchController,
|
||||||
} from '../../includes/chooserModal';
|
} from '../../includes/chooserModal';
|
||||||
import { gettext } from '../../utils/gettext';
|
|
||||||
|
|
||||||
function ajaxifyImageUploadForm(modal) {
|
function ajaxifyImageUploadForm(modal) {
|
||||||
$('form[data-chooser-modal-creation-form]', modal.body).on(
|
$('form[data-chooser-modal-creation-form]', modal.body).on(
|
||||||
'submit',
|
'submit',
|
||||||
(event) => {
|
(event) => {
|
||||||
let hasErrors = false;
|
if (validateCreationForm(event.currentTarget)) {
|
||||||
for (const input of event.currentTarget.querySelectorAll(
|
|
||||||
'input[required]',
|
|
||||||
)) {
|
|
||||||
if (!input.value) {
|
|
||||||
hasErrors = true;
|
|
||||||
const li = input.closest('li');
|
|
||||||
if (!li.classList.contains('error')) {
|
|
||||||
li.classList.add('error');
|
|
||||||
const container = input.closest('.field-content');
|
|
||||||
const errorPara = document.createElement('p');
|
|
||||||
errorPara.className = 'error-message';
|
|
||||||
container.appendChild(errorPara);
|
|
||||||
const errorSpan = document.createElement('span');
|
|
||||||
errorPara.appendChild(errorSpan);
|
|
||||||
errorSpan.innerText = gettext('This field is required.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (hasErrors) {
|
|
||||||
// eslint-disable-next-line no-undef
|
|
||||||
setTimeout(cancelSpinner, 500);
|
|
||||||
} else {
|
|
||||||
submitCreationForm(modal, event.currentTarget, {
|
submitCreationForm(modal, event.currentTarget, {
|
||||||
errorContainerSelector: '#tab-upload',
|
errorContainerSelector: '#tab-upload',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -4,6 +4,31 @@ import { initTabs } from './tabs';
|
||||||
import { initTooltips } from './initTooltips';
|
import { initTooltips } from './initTooltips';
|
||||||
import { gettext } from '../utils/gettext';
|
import { gettext } from '../utils/gettext';
|
||||||
|
|
||||||
|
const validateCreationForm = (form) => {
|
||||||
|
let hasErrors = false;
|
||||||
|
form.querySelectorAll('input[required]').forEach((input) => {
|
||||||
|
if (!input.value) {
|
||||||
|
hasErrors = true;
|
||||||
|
const li = input.closest('li');
|
||||||
|
if (!li.classList.contains('error')) {
|
||||||
|
li.classList.add('error');
|
||||||
|
const container = input.closest('.field-content');
|
||||||
|
const errorPara = document.createElement('p');
|
||||||
|
errorPara.className = 'error-message';
|
||||||
|
container.appendChild(errorPara);
|
||||||
|
const errorSpan = document.createElement('span');
|
||||||
|
errorPara.appendChild(errorSpan);
|
||||||
|
errorSpan.innerText = gettext('This field is required.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (hasErrors) {
|
||||||
|
// eslint-disable-next-line no-undef
|
||||||
|
setTimeout(cancelSpinner, 500);
|
||||||
|
}
|
||||||
|
return !hasErrors;
|
||||||
|
};
|
||||||
|
|
||||||
const submitCreationForm = (modal, form, { errorContainerSelector }) => {
|
const submitCreationForm = (modal, form, { errorContainerSelector }) => {
|
||||||
const formdata = new FormData(form);
|
const formdata = new FormData(form);
|
||||||
|
|
||||||
|
@ -211,10 +236,11 @@ class ChooserModalOnloadHandlerFactory {
|
||||||
ajaxifyCreationForm(modal) {
|
ajaxifyCreationForm(modal) {
|
||||||
/* Convert the creation form to an AJAX submission */
|
/* Convert the creation form to an AJAX submission */
|
||||||
$(this.creationFormSelector, modal.body).on('submit', (event) => {
|
$(this.creationFormSelector, modal.body).on('submit', (event) => {
|
||||||
submitCreationForm(modal, event.currentTarget, {
|
if (validateCreationForm(event.currentTarget)) {
|
||||||
errorContainerSelector: this.creationFormTabSelector,
|
submitCreationForm(modal, event.currentTarget, {
|
||||||
});
|
errorContainerSelector: this.creationFormTabSelector,
|
||||||
|
});
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -287,6 +313,7 @@ const chooserModalOnloadHandlers =
|
||||||
new ChooserModalOnloadHandlerFactory().getOnLoadHandlers();
|
new ChooserModalOnloadHandlerFactory().getOnLoadHandlers();
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
validateCreationForm,
|
||||||
submitCreationForm,
|
submitCreationForm,
|
||||||
initPrefillTitleFromFilename,
|
initPrefillTitleFromFilename,
|
||||||
SearchController,
|
SearchController,
|
||||||
|
|
Ładowanie…
Reference in New Issue