Skip to content

Adding Calculations to Collection Form #736

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

You must be logged in to vote

Hi @GronsoBitburg,

That sounds like a good use for hooks.

import { CollectionBeforeChangeHook } from 'payload/types'

const healthCheckCalculations: CollectionBeforeChangeHook : CollectionBeforeChangeHook  = ({data}) => {
  data.bmi = data.weight / data.height / data.height * 10000;
  data.waisttohips_ratio = data.waist / data.hips;
}

const userHealthCheck: CollectionConfig = {
  slug: 'userhealthcheck',
  hooks: {
    beforeChange: [ healthCheckCalculations ],
  },
  // rest of the collection
}

By the way, that read access () => true means that everybody including guest users who have not logged in can query and read your data on this collection. I want to be sure you know that since it…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
4 replies
@GronsoBitburg
Comment options

@DanRibbens
Comment options

@GronsoBitburg
Comment options

@DanRibbens
Comment options

Answer selected by GronsoBitburg
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