Adding Calculations to Collection Form #736
-
I am struggling to calculate fields which would benefit from being worked out before submission. I can see there is I can't find any information on how to use math to automate some of the fields, but I assume that it's more than capable in PayloadCMS. I'm just getting to grips with js, react and typescript, so I'm not well versed in the correct syntax and structure of the code just yet, so just keep making spaghetti code and creating a tonne of errors and not getting anywhere very quickly. How would I go about grabbing 2 or 3 numerical fields to then calculate to find the total? This is an example of my code. I'm basically trying to do pretty simple maths, but I don't know where to pull my data from or how to calculate it. Can anyone give me some pointers? import {
CollectionConfig,
} from 'payload/types';
const userHealthCheck: CollectionConfig = {
slug: 'userhealthcheck',
admin: {
defaultColumns: ['name', 'height', 'weight', 'hips', 'waist'],
useAsTitle: 'title',
},
access: {
read: () => true,
},
fields: [{
name: 'name',
type: 'text',
label: 'Name'
},
{
name: 'height',
type: 'number',
label: 'Height in cm'
},
{
name: 'weight',
type: 'number',
label: 'Weight in kg'
},
{
name: 'hips',
type: 'number',
label: 'Hips'
},
{
name: 'waist',
type: 'number',
label: 'Waist'
},
{
name: 'waisttohips_ratio',
type: 'number',
label: 'Waist to Hips Ratio'
//Calculate Waist / Hips = Waist Hips ratio
},
{
name: 'bmi',
type: 'number',
label: 'BMI',
//Calculate [weight (kg) / height (cm) / height (cm)] x 10,000
}
]
}
export default userHealthCheck; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
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 sounds like some personal information. |
Beta Was this translation helpful? Give feedback.
-
Wait... It's that easy? I was really over complicating it Thank you so much for taking the time to help. I was thinking that it was gonna be a lot more complicated to get that working. The read access () => true is just so I can play around with payload as I'll have to figure out the api section (when I get to that bit). This project won't go any further than my intranet so I'm pretty comfortable with that. To be honest, I should have removed that for any new user reading my post. |
Beta Was this translation helpful? Give feedback.
Hi @GronsoBitburg,
That sounds like a good use for hooks.
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…