Required Relation Field
Enforces required relation fields in Strapi to be non-empty on save or publish
Strapi Plugin: Required Relation Field
Enforces required relation fields in Strapi to be non-empty on save or publish.
Installation
You just need to install the strapi-plugin-required-relation-field
package via npm
or yarn
at the root of your Strapi project:
npm:
npm i strapi-plugin-required-relation-field
yarn:
yarn add strapi-plugin-required-relation-field
Usage
Simply set required: true
for relations in your schemas, and everything will work out-of-the-box. For example:
1# src/api/story-reaction/content-types/story-reaction/schema.json
2{
3 "kind": "collectionType",
4 "collectionName": "story_reactions",
5 "info": {
6 "singularName": "story-reaction",
7 "pluralName": "story-reactions",
8 "displayName": "Story Reaction",
9 "description": ""
10 },
11 "options": {
12 "draftAndPublish": false
13 },
14 "attributes": {
15 "story": {
16 "type": "relation",
17 "relation": "manyToOne",
18 "target": "api::story.story",
19 "inversedBy": "storyReactions",
20 "required": true
21 },
22 "user": {
23 "type": "relation",
24 "relation": "manyToOne",
25 "target": "plugin::users-permissions.user",
26 "inversedBy": "storyReactions",
27 "required": true
28 },
29 "reaction": {
30 "type": "relation",
31 "relation": "manyToOne",
32 "target": "api::reaction.reaction",
33 "inversedBy": "storyReactions",
34 "required": true
35 }
36 }
37}
Development
To test the plugin during development, build it with the watch:link
command:
yarn watch:link
Next, link it to a local Strapi project. In a new terminal window, run:
cd /path/to/strapi/project
yarn dlx yalc add --link strapi-plugin-required-relation-field
yarn install
Since this plugin is installed via node_modules
, you don't need to explicitly add it to your plugins
configuration file. Running the develop
command will automatically detect your plugin.
Start your Strapi project with:
yarn develop
You are now ready to develop your plugin! Note that server changes require a server restart to take effect.
Install now
npm install strapi-plugin-required-relation-field
Create your own plugin
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.