form
form copied to clipboard
How to validate sub fields of two arrays?
I have two fields that are arrays of user objects. How can i validate that user.name are unique across both arrays?
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;
},
}}