Validation on API for collectionLink ID

I have a collection (collection A) that have a relation with a another collection (Collection B). using a collectionLink field type.

When i uses API to add entries to Collection A, If i send a value for “_id” for “Collection B” , it will be posted even it is a wrong id ( non existing Id on collection B)

Is there any way to throw error or at least to prevent adding a wrong id on CollectionLink ?

Thanks

@Mohamed you need to add a validation in a collections.save.before.<colname>, something similar to below:

$app->on('collections.save.before.col_name', function($name, &$entry, $isUpdate) use($app) {
  $id = $entry['linked_field_name']['_id'];
  $linked = $app->module('collections')->findOne('linked_col_name', ['_id' => $id]);
  if (!$linked) {
    // stop and return error
  }
});
1 Like

@pauloamgomes Thank you for support!:slightly_smiling_face: