diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/App/actions.js b/packages/strapi-plugin-content-type-builder/admin/src/containers/App/actions.js index b1a4dd5e5a..12263d2fcc 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/App/actions.js +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/App/actions.js @@ -306,7 +306,11 @@ export function updateTempContentType() { // utils export const buildModelAttributes = attributes => { const formattedAttributes = attributes.reduce((acc, current) => { - acc[current.name] = current.params; + if (current.params.type === 'enumeration') { + acc[current.name] = Object.assign(current.params, { enum: current.params.enum.join(',') }); + } else { + acc[current.name] = current.params; + } return acc; }, {}); @@ -331,6 +335,8 @@ export const formatModelAttributes = attributes => if (curr === 'plugin' && !!value) { acc2.params.pluginValue = value; acc2.params.plugin = true; + } else if (curr === 'enum') { + acc2.params.enum = value.split(','); } else { acc2.params[curr] = value; } 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 6b4dac830f..2dc54c8de6 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 @@ -41,8 +41,9 @@ class AttributeForm extends React.Component { getFormErrors = () => { const { alreadyTakenAttributes, attributeToEditName, modifiedData } = this.props; - const currentForm = this.getCurrentForm(); + let formErrors = {}; + const formValidations = this.getFormValidations(); const alreadyTakenAttributesUpdated = alreadyTakenAttributes.filter( attribute => attribute !== attributeToEditName, ); @@ -55,14 +56,15 @@ class AttributeForm extends React.Component { formErrors = { name: [{ id: `${pluginId}.error.attribute.taken` }] }; } - // TODO NEED TO HANDLE OTHER VALIDATIONS - formErrors = Object.keys(modifiedData).reduce((acc, current) => { - const { custom, validations } = currentForm.find(input => input.name === current) || { - validations: {}, - }; + formErrors = Object.keys(formValidations).reduce((acc, current) => { + const { custom, validations } = formValidations[current]; const value = modifiedData[current]; - if (validations.required === true && value === '' && custom === true) { + if (!value && validations.required === true && custom !== true) { + acc[current] = [{ id: `${pluginId}.error.validation.required` }]; + } + + if (custom === true && validations.required === true && value === '') { acc[current] = [{ id: `${pluginId}.error.validation.required` }]; } @@ -77,6 +79,22 @@ class AttributeForm extends React.Component { return formErrors; }; + getFormValidations = () => { + const { attributeType } = this.props; + const form = supportedAttributes[attributeType]; + + return Object.keys(form).reduce((acc, current) => { + return { + ...acc, + ...form[current].items.reduce((acc2, curr) => { + acc2[curr.name] = { validations: curr.validations, custom: curr.custom }; + + return acc2; + }, {}), + }; + }, {}); + }; + handleCancel = () => { const { push } = this.props; diff --git a/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/supportedAttributes.json b/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/supportedAttributes.json index 23302d08f5..59e3b1c3e8 100644 --- a/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/supportedAttributes.json +++ b/packages/strapi-plugin-content-type-builder/admin/src/containers/AttributeForm/supportedAttributes.json @@ -219,7 +219,7 @@ "label": { "id": "content-type-builder.form.attribute.item.enumeration.rules" }, - "name": "enumValue", + "name": "enum", "type": "textarea", "placeholder": "content-type-builder.form.attribute.item.enumeration.placeholder", "value": false,