hooks property in your Field Config:
Config Options
All Field Hooks accept an array of synchronous or asynchronous functions. These functions can optionally modify the return value of the field before the operation continues. All Field Hooks are formatted to accept the same arguments, although some arguments may beundefined based on the specific hook type.
To add hooks to a Field, use the hooks property in your Field Config:
Each hook returns the field’s new value, or
undefined to keep the current value.
Field Hooks receive the following arguments. Optional values depend on the hook phase and operation:
collection is null for a Global field. siblingFields is optional in the base FieldHookArgs type because afterChange does not receive it.
beforeValidate
Runs during thecreate and update operations. This hook allows you to add or format data before the incoming data is validated server-side.
Please do note that this does not run before client-side validation. If you render a custom field component in your front-end and provide it with a validate function, the order that validations will run in is:
validateruns on the client- if successful,
beforeValidateruns on the server validateruns on the server
beforeValidate hook is used to process the username field. The hook takes the incoming value of
the field and transforms it by trimming whitespace and converting it to lowercase. This ensures that the username is
stored in a consistent format in the database.
beforeChange
Immediately before validation, beforeChange hooks will run during create and update operations. At this stage, the data should be treated as unvalidated user input. There is no guarantee that required fields exist or that fields are in the correct format. As such, using this data for side effects requires manual validation. You can optionally modify the shape of the data to be saved.emailField, the beforeChange hook checks the operation type. If the operation is create, it performs
additional validation or transformation on the email field value. This allows for operation-specific logic to be applied
to the field.
afterChange
TheafterChange hook is executed after a field’s value has been changed and saved in the database. This hook is useful
for post-processing or triggering side effects based on the new value of the field.
afterChange hook is used with a membershipStatusField, which allows users to select their
membership level (Standard, Premium, VIP). The hook monitors changes in the membership status. When a change occurs, it
logs the update and can be used to trigger further actions, such as tracking conversion from one tier to another or
notifying them about changes in their membership benefits.
afterRead
TheafterRead hook is invoked after a field value is read from the database. This is ideal for formatting or
transforming the field data for output.
afterRead hook for the dateField is used to format the date into a more readable format
using toLocaleDateString(). This hook modifies the way the date is presented to the user, making it more
user-friendly.
beforeDuplicate
ThebeforeDuplicate field hook is called on each locale (when using localization), when duplicating a document. It may be used when documents having the
exact same properties may cause issue. This gives you a way to avoid duplicate names on unique, required fields or when external systems expect non-repeating values on documents.
This hook gets called before the beforeValidate and beforeChange hooks are called.
By Default, unique and required text fields FrogBot will append ”- Copy” to the original document value. The default is not added if your field has its own, you must return non-unique values from your beforeDuplicate hook to avoid errors or enable the disableDuplicate option on the collection.
Here is an example of a number field with a hook that increments the number to avoid unique constraint errors when duplicating a document: