Refactor form data validation to extra function and run on every change
[MAILPOET-2455]
This commit is contained in:
committed by
Jack Kitterhing
parent
74d00145be
commit
c0b3ccfecc
@ -7,11 +7,12 @@ export default {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
dispatch('mailpoet-form-editor').removeNotice('save-form');
|
dispatch('mailpoet-form-editor').removeNotice('save-form');
|
||||||
const formData = select('mailpoet-form-editor').getFormData();
|
const formErrors = select('mailpoet-form-editor').getFormErrors();
|
||||||
if (!formData.settings.segments || formData.settings.segments.length === 0) {
|
if (formErrors.includes('missing-lists')) {
|
||||||
dispatch('mailpoet-form-editor').addDismissibleNotice(MailPoet.I18n.t('settingsPleaseSelectList'), 'error', 'missing-lists');
|
dispatch('mailpoet-form-editor').addDismissibleNotice(MailPoet.I18n.t('settingsPleaseSelectList'), 'error', 'missing-lists');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const formData = select('mailpoet-form-editor').getFormData();
|
||||||
dispatch('mailpoet-form-editor').saveFormStarted();
|
dispatch('mailpoet-form-editor').saveFormStarted();
|
||||||
dispatch('mailpoet-form-editor').removeNotice('missing-lists');
|
dispatch('mailpoet-form-editor').removeNotice('missing-lists');
|
||||||
MailPoet.Ajax.post({
|
MailPoet.Ajax.post({
|
||||||
|
7
assets/js/src/form_editor/store/form_validator.jsx
Normal file
7
assets/js/src/form_editor/store/form_validator.jsx
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export default (formData) => {
|
||||||
|
const errors = [];
|
||||||
|
if (!formData.settings.segments || formData.settings.segments.length === 0) {
|
||||||
|
errors.push('missing-lists');
|
||||||
|
}
|
||||||
|
return errors;
|
||||||
|
};
|
@ -1,7 +1,15 @@
|
|||||||
export default (state, action) => ({
|
import validateForm from '../form_validator.jsx';
|
||||||
|
|
||||||
|
export default (state, action) => {
|
||||||
|
const newState = {
|
||||||
...state,
|
...state,
|
||||||
formData: {
|
formData: {
|
||||||
...state.formData,
|
...state.formData,
|
||||||
settings: action.settings,
|
settings: action.settings,
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
return {
|
||||||
|
...newState,
|
||||||
|
formErrors: validateForm(newState.formData),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
@ -35,4 +35,7 @@ export default {
|
|||||||
getNotice(state, id) {
|
getNotice(state, id) {
|
||||||
return state.notices.find((notice) => notice.id === id);
|
return state.notices.find((notice) => notice.id === id);
|
||||||
},
|
},
|
||||||
|
getFormErrors(state) {
|
||||||
|
return state.formErrors;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
@ -7,11 +7,13 @@ import * as actions from './actions.jsx';
|
|||||||
import createReducer from './reducer.jsx';
|
import createReducer from './reducer.jsx';
|
||||||
import selectors from './selectors.jsx';
|
import selectors from './selectors.jsx';
|
||||||
import controls from './controls.jsx';
|
import controls from './controls.jsx';
|
||||||
|
import validateForm from './form_validator.jsx';
|
||||||
|
|
||||||
const defaultState = {
|
const defaultState = {
|
||||||
sidebarOpened: true,
|
sidebarOpened: true,
|
||||||
formData: window.mailpoet_form_data,
|
formData: window.mailpoet_form_data,
|
||||||
formExports: window.mailpoet_form_exports,
|
formExports: window.mailpoet_form_exports,
|
||||||
|
formErrors: validateForm(window.mailpoet_form_data),
|
||||||
segments: window.mailpoet_form_segments,
|
segments: window.mailpoet_form_segments,
|
||||||
pages: window.mailpoet_form_pages,
|
pages: window.mailpoet_form_pages,
|
||||||
isFormSaving: false,
|
isFormSaving: false,
|
||||||
|
Reference in New Issue
Block a user