Update form validator to be able to check email and submit are present columns
[MAILPOET-2754]
This commit is contained in:
committed by
Veljko V
parent
fb5df4b09e
commit
5c20c11ac9
@@ -7,6 +7,7 @@ import formatCustomFieldBlockName from '../blocks/format_custom_field_block_name
|
|||||||
import getCustomFieldBlockSettings from '../blocks/custom_fields_blocks.jsx';
|
import getCustomFieldBlockSettings from '../blocks/custom_fields_blocks.jsx';
|
||||||
import { registerCustomFieldBlock } from '../blocks/blocks.jsx';
|
import { registerCustomFieldBlock } from '../blocks/blocks.jsx';
|
||||||
import mapFormDataBeforeSaving from './map_form_data_before_saving.jsx';
|
import mapFormDataBeforeSaving from './map_form_data_before_saving.jsx';
|
||||||
|
import findBlock from './find_block.jsx';
|
||||||
|
|
||||||
const formatApiErrorMessage = (response) => {
|
const formatApiErrorMessage = (response) => {
|
||||||
let errorMessage = null;
|
let errorMessage = null;
|
||||||
@@ -17,21 +18,6 @@ const formatApiErrorMessage = (response) => {
|
|||||||
return errorMessage;
|
return errorMessage;
|
||||||
};
|
};
|
||||||
|
|
||||||
const findBlock = (blocks, name) => (
|
|
||||||
blocks.reduce((result, block) => {
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if (block.name === name) {
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
if (Array.isArray(block.innerBlocks) && block.innerBlocks.length) {
|
|
||||||
return findBlock(block.innerBlocks, name);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}, null)
|
|
||||||
);
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
SAVE_FORM() {
|
SAVE_FORM() {
|
||||||
if (select('mailpoet-form-editor').getIsFormSaving()) {
|
if (select('mailpoet-form-editor').getIsFormSaving()) {
|
||||||
|
16
assets/js/src/form_editor/store/find_block.jsx
Normal file
16
assets/js/src/form_editor/store/find_block.jsx
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
const findBlock = (blocks, name) => (
|
||||||
|
blocks.reduce((result, block) => {
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (block.name === name) {
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
if (Array.isArray(block.innerBlocks) && block.innerBlocks.length) {
|
||||||
|
return findBlock(block.innerBlocks, name);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}, null)
|
||||||
|
);
|
||||||
|
|
||||||
|
export default findBlock;
|
@@ -1,3 +1,5 @@
|
|||||||
|
import findBlock from './find_block.jsx';
|
||||||
|
|
||||||
export default (formData, formBlocks) => {
|
export default (formData, formBlocks) => {
|
||||||
if (!formData || !formData.settings || !Array.isArray(formData.settings.segments)) {
|
if (!formData || !formData.settings || !Array.isArray(formData.settings.segments)) {
|
||||||
throw new Error('formData.settings.segments are expected to be an array.');
|
throw new Error('formData.settings.segments are expected to be an array.');
|
||||||
@@ -9,8 +11,8 @@ export default (formData, formBlocks) => {
|
|||||||
if (!formData.settings.segments || formData.settings.segments.length === 0) {
|
if (!formData.settings.segments || formData.settings.segments.length === 0) {
|
||||||
errors.push('missing-lists');
|
errors.push('missing-lists');
|
||||||
}
|
}
|
||||||
const emailInput = formBlocks.find((block) => (block.name === 'mailpoet-form/email-input'));
|
const emailInput = findBlock(formBlocks, 'mailpoet-form/email-input');
|
||||||
const submit = formBlocks.find((block) => (block.name === 'mailpoet-form/submit-button'));
|
const submit = findBlock(formBlocks, 'mailpoet-form/submit-button');
|
||||||
if (!emailInput) {
|
if (!emailInput) {
|
||||||
errors.push('missing-email-input');
|
errors.push('missing-email-input');
|
||||||
}
|
}
|
||||||
|
@@ -21,6 +21,27 @@ const submitBlock = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const columns = {
|
||||||
|
clientId: 'columns-1',
|
||||||
|
name: 'core/columns',
|
||||||
|
isValid: true,
|
||||||
|
attributes: {
|
||||||
|
verticalAlignment: 'center',
|
||||||
|
},
|
||||||
|
innerBlocks: [
|
||||||
|
{
|
||||||
|
clientId: 'column-1-1',
|
||||||
|
name: 'core/column',
|
||||||
|
isValid: true,
|
||||||
|
attributes: {
|
||||||
|
width: 66.66,
|
||||||
|
verticalAlignment: 'center',
|
||||||
|
},
|
||||||
|
innerBlocks: [emailBlock, submitBlock],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
describe('Form validator', () => {
|
describe('Form validator', () => {
|
||||||
it('Should return no errors for valid data', () => {
|
it('Should return no errors for valid data', () => {
|
||||||
const formData = {
|
const formData = {
|
||||||
@@ -33,6 +54,17 @@ describe('Form validator', () => {
|
|||||||
expect(result).to.be.empty;
|
expect(result).to.be.empty;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should validate form data with nested email and submit', () => {
|
||||||
|
const formData = {
|
||||||
|
settings: {
|
||||||
|
segments: [1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const blocks = [columns];
|
||||||
|
const result = validate(formData, blocks);
|
||||||
|
expect(result).to.be.empty;
|
||||||
|
});
|
||||||
|
|
||||||
it('Should return error for missing lists', () => {
|
it('Should return error for missing lists', () => {
|
||||||
const formData = {
|
const formData = {
|
||||||
settings: {
|
settings: {
|
||||||
|
Reference in New Issue
Block a user