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

default discord avatar
Sasuke14120
last month
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
    Payload Team
    last month

    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',
        },
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

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