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

default discord avatar
Sasuke1412011 months ago
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
    11 months ago

    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.