Skip to content

How to implement automatic Custom ID? #5878

Answered by DanRibbens
didiraja asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @didiraja,

I was surprised that defaultValue is not allowed for custom ID fields. As another workaround you could add it to the beforeValidate hook like this:

// in your collection:
  hooks: {
    beforeValidate: [({ data }) => {
      if (!data.id) {
        // replace with your own way to generate IDs
        const customID = uuid()
        return {...data, id: customID }
      }
      return data
    }],
  },
  fields: [
    {
      name: 'id',
      type: 'text',
      admin: {
        components: {
          Field: () => null,
        },
      },
    },
 // ... rest of the fields
}]

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@DanRibbens
Comment options

@didiraja
Comment options

Answer selected by didiraja
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants