diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/index.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/index.js index ded091acc8..73444970c8 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/index.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/index.js @@ -74,6 +74,13 @@ class AttributeForm extends React.Component { const { custom, validations } = formValidations[current]; const value = modifiedData[current]; + if ( + current === 'name' && + !new RegExp('^[A-Za-z][_0-9A-Za-z]*$').test(value) + ) { + acc[current] = [{ id: `${pluginId}.error.validation.regex.name` }]; + } + if (!value && validations.required === true && custom !== true) { acc[current] = [{ id: `${pluginId}.error.validation.required` }]; } @@ -82,13 +89,6 @@ class AttributeForm extends React.Component { acc[current] = [{ id: `${pluginId}.error.validation.required` }]; } - if ( - current === 'name' && - !new RegExp('^[A-Za-z][_0-9A-Za-z]*$').test(value) - ) { - acc[current] = [{ id: `${pluginId}.error.validation.regex.name` }]; - } - if (current === 'enum' && !!value) { const split = value.split('\n'); diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/tests/index.test.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/tests/index.test.js index 6e64444169..4f234689cd 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/tests/index.test.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/tests/index.test.js @@ -79,7 +79,7 @@ describe('', () => { wrapper .find(FormattedMessage) .at(0) - .prop('id'), + .prop('id') ).toContain('edit'); }); @@ -101,7 +101,7 @@ describe('', () => { describe('instances', () => { describe('GetFormErrors', () => { - it('should return an empty object if there is not field that contain the created field\'s name', () => { + it("should return an empty object if there is not field that contain the created field's name", () => { props.modifiedData = { name: 'test' }; wrapper = renderComponent(props); @@ -111,7 +111,7 @@ describe('', () => { expect(getFormErrors()).toEqual({}); }); - it('should return an object with the input\'s name and an array of error if the name is empty', () => { + it("should return an object with the input's name and an array of error if the name is empty", () => { wrapper = renderComponent(props); const { getFormErrors } = wrapper.instance(); @@ -121,6 +121,18 @@ describe('', () => { }); }); + it('should return a unique error if the name begins with a special character', () => { + props.modifiedData = { name: '_test' }; + + wrapper = renderComponent(props); + + const { getFormErrors } = wrapper.instance(); + + expect(getFormErrors()).toEqual({ + name: [{ id: `${pluginId}.error.validation.regex.name` }], + }); + }); + it('should return a unique error if the name of the field is already taken', () => { props.alreadyTakenAttributes = ['test']; props.modifiedData = { name: 'test' }; @@ -200,7 +212,7 @@ describe('', () => { 'modalType=attributeForm&attributeType=string&settingType=advanced&actionType=create', }); expect(context.emitEvent).toHaveBeenCalledWith( - 'didSelectContentTypeFieldSettings', + 'didSelectContentTypeFieldSettings' ); }); }); @@ -292,7 +304,7 @@ describe('', () => { expect(props.onSubmitEdit).toHaveBeenCalledWith(true); expect(context.emitEvent).toHaveBeenCalledWith( - 'willAddMoreFieldToContentType', + 'willAddMoreFieldToContentType' ); }); diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/ModelForm/index.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/ModelForm/index.js index 6c0642f11e..e634dec0b9 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/ModelForm/index.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/ModelForm/index.js @@ -100,16 +100,16 @@ class ModelForm extends React.Component { }; } - if (modifiedData.name === '') { - formErrors = { name: [{ id: `${pluginId}.error.validation.required` }] }; - } - if (!new RegExp('^[A-Za-z][_0-9A-Za-z]*$').test(modifiedData.name)) { formErrors = { name: [{ id: `${pluginId}.error.validation.regex.name` }], }; } + if (modifiedData.name === '') { + formErrors = { name: [{ id: `${pluginId}.error.validation.required` }] }; + } + this.setState(prevState => ({ formErrors, didCheckErrors: !prevState.didCheckErrors, diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/ModelForm/tests/index.test.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/ModelForm/tests/index.test.js index 890ad9cbb2..573361ea37 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/ModelForm/tests/index.test.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/ModelForm/tests/index.test.js @@ -254,6 +254,8 @@ describe('', () => { describe('HandleSubmit', () => { it('should not submit if the form is empty', () => { + props.modifiedData.name = ''; + wrapper = renderComponent(props); const { handleSubmit } = wrapper.instance(); @@ -266,6 +268,21 @@ describe('', () => { expect(wrapper.prop('push')).not.toHaveBeenCalled(); }); + it('should not submit if the name of the CT begins with a special character', () => { + props.modifiedData.name = '_test'; + + wrapper = renderComponent(props); + + const { handleSubmit } = wrapper.instance(); + + handleSubmit({ preventDefault: jest.fn() }); + + expect(wrapper.state('formErrors')).toEqual({ + name: [{ id: `${pluginId}.error.validation.regex.name` }], + }); + expect(wrapper.prop('push')).not.toHaveBeenCalled(); + }); + it('should not submit if the name of the CT is already taken', () => { props.allTakenNames = ['test']; props.modifiedData.name = 'test';