Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Use a user-inputted field value to generate another field value

default discord avatar
Sasuke14120last year
1

Can we use a field value entered by the user to generate another field value in the same collection? Example: User entered a value in full name field, can we use the this value to fetch the first name ?

  • discord user avatar
    denolfe
    last year

    You can perform this type of operation with a field hook. Here is an example of a beforeChange hook to populate a

    firstName

    field by splitting the

    fullName

    value.



        {
          name: 'fullName',
          type: 'text',
          hooks: {
            beforeChange: [
              ({ data, value, operation }) => {
                data.firstName = typeof value === 'string' ? value.split(' ')[0] : '';
                return value;
              },
            ],
          },
        },
        {
          name: 'firstName',
          type: 'text',
        },
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.