form icon indicating copy to clipboard operation
form copied to clipboard

How to validate sub fields of two arrays?

Open da1z opened this issue 9 months ago • 1 comments

I have two fields that are arrays of user objects. How can i validate that user.name are unique across both arrays?

da1z avatar Mar 15 '25 13:03 da1z

Seems to be doable with Linked fields.

You can listen to other fields on change, for example:

validators={{
  onChangeListenTo: ['people'],
  onChange: ({ value, fieldApi }) => {
    if (!value) {
      return undefined;
    }
    const valueExists = fieldApi.form
      .getFieldValue('people')
      .some(
        ({ name }) => name === value
      );

    if (valueExists) {
      return 'Name must be unique';
    }

    return undefined;
  },
}}

MVaik avatar Mar 15 '25 14:03 MVaik