Contentful logo

Contentful Community

Update validation schema of a field in an exiting form

Hello,

I really have a lot of difficulty redefining the validation schema of a field in an exiting form.

The idea is to define a required field according to the choice of a drop-down list. I am using an extension app with a DropdownEditor (field-editor) for DropDownList and I get the value I choose from sdk.field.onValueChanged

For example: If I choose ‘Accepted’ in the drop-down list the StartDate field must be required and I click on Publish, I would like to have a publication error and the error below the StartDate field

1st try (not working):

sdk.entry.fields.startDate.required = true;

2nd try (not working):

const startDate = sdk.contentType.fields.find((f) => f.id === ‘startDate’);
startDate.required = true;
sdk.contentType.fields = sdk.contentType.fields.map ((m) => (m.id! == startDate.id? m: startDate));

3rd try (error VesrionMisMatch):

const startDate = sdk.contentType.fields.find((f) => f.id === ‘startDate’);
startDate.required = true;
sdk.contentType.fields = sdk.contentType.fields.map ((m) => (m.id! == startDate.id? m: startDate));
updateContentType (sdk.contentType);

4th try (success 200 but not working after click publish button):

async function updateContentType (contentTypeUpdate: any) {
// Update content type
customer
.getSpace (‘space_id’)
.then ((space) => space.getEnvironment (‘env-id’))
.then ((environment) => environment.getContentType (contentTypeId))
.then ((contentType) => {
contentType.fields = contentTypeUpdate.fields;
return contentType.update ();
})
.then ((contentType) => console.log (Content type $ {contentType.sys.id} renamed.))
.catch (console.error);
}
const startDate = sdk.contentType.fields.find((f) => f.id === ‘startDate’);
startDate.required = true;
sdk.contentType.fields = sdk.contentType.fields.map ((m) => (m.id! == startDate.id? m: startDate));
updateContentType (sdk.contentType);

Could you help me please ?

I managed to update the ContentType with SDK in this way

const contentType = (await sdk.space.getContentType(sdk.ids.contentType)) as ContentTypeProps;
const startDate = sdk.contentType.fields.find((f) => f.id === ‘startDate’);
if (startDate) {
const required = value === ‘Accepted’;
sdk.entry.fields.startDate.required = required;
startDate.required = required;
contentType.fields = sdk.contentType.fields.map((field) => (field.id === startDate.id ? startDate : field));
await sdk.space.updateContentType(contentType);
}

Here I was missing the publish with contentful-management,

async function updateContentType (contentTypeUpdate: any) {
// Update content type
customer
.getSpace (‘space_id’)
.then ((space) => space.getEnvironment (‘env-id’))
.then ((environment) => environment.getContentType (contentTypeId))
.then ((contentType) => {
contentType.fields = contentTypeUpdate.fields;
return contentType.update ();
})
.then ((contentType) => contentType.publish())
.catch (console.error);
}
const startDate = sdk.contentType.fields.find((f) => f.id === ‘startDate’);
startDate.required = true;
sdk.contentType.fields = sdk.contentType.fields.map ((m) => (m.id! == startDate.id? m: startDate));
updateContentType (sdk.contentType);

I hope it can help someone.

But now I have a concern of refresh in my StartDate component the (required) is not displayed dynamically. If you have any idea.

After a lot of research and discussion with the contentful team. I made a UI extension (app) to manage the 3 elements, including the Dropdown, the startDate and the EndDate