I’m sorry I don’t exactly follow your example, but I suspect what you need to use is the validation in the table model. And for the second table you may need to load it in that routine. I’ll try to hack this together and no doubt someone will correct it as I’m not sure this is an optimal approach (loading a model on the fly), but here goes!
src/Model/Table/MembresTable.php have something like: -
public function validationDefault(Validator $validator): Validator
->add('nb_annee_adhesion', 'custom', [
'rule' => function($value, $context)
$query = $this->Types->find()->where([ 'id' => $context['data']['type_id'] ]);
$data = $query->toArray();
return $value === $data['id'];
'message' => __('Types are not valid'),
I don’t even know if you can do that, load a model in the validator and use its field to compare - I think the SQL could be made smarter instead.
May be this will help Validating Data - 4.x (I assume you’re using the latest version of Cake as version wasn’t mentioned). My syntax is almost certainly wrong and I guessed some field names - but its a start!