Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to validate sub fields of two arrays? #1281

Open
da1z opened this issue Mar 15, 2025 · 1 comment
Open

How to validate sub fields of two arrays? #1281

da1z opened this issue Mar 15, 2025 · 1 comment

Comments

@da1z
Copy link

da1z commented Mar 15, 2025

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

@MVaik
Copy link

MVaik commented Mar 15, 2025

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;
  },
}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants