I have two different content-types. Both content type has a text field called url
. Is it possible to add a validation to the field that checks that the value is unique across both content-types?
Hi @gwehmeier,
AFAIK unique validity checks are always performed within the scope of the content type.
You could work around that limitation with a UI Extension. That would work when editing content via the Contentful web app. It would not prevent API changes to apply the same value twice though (as Teemu pointed out)
You could also abstract out the common content from the two content types and make a shared content type that then links to an entry of the two content types. Then you could enforce uniqueness on that shared content type.
Initial setup
Imagine you have two content types: Article
and Post
. Starting out, the Article
content type has:
- a
Name
short text field - a
Slug
short text field - an
Article Body
rich-text field
And the Post
content type has:
- a
Name
short text field - a
Slug
short text field - an
Author
single reference field - a
Post Body
long text field
Improvement
To make it possible to validate the uniqueness of your slugs used by your Posts and Articles, you could make another content type called Navigation
which has:
- a
Name
short text field - a
Slug
short text field, which has the uniqueness validation enabled - a
Content
single reference field
And remove the slug
field from both Post
and Article
content types. (Keep the Name
field because it’s helpful for everything in Contentful to have an entry title even if it is just for internal purposes.)
With this setup you could enforce uniqueness and be able to query Contentful for the single piece of content that should exist at a particular URL.