How to do custom validatation in a model comparing 2 fields

Validation in table “membres”
I want to compare contain of a field #1: " type_id" : [Index] pointing to a table “types”, containing 4 entries (ie) 0;1;2;3;4 with
another field #2 : “nb_annee_adhesion” containing values 0;1;2;3 ;

For example: if contain of field#1 is = 1 and field#2 = 0, then error message else continue.
Thanks

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!

In your src/Model/Table/MembresTable.php have something like: -

    public function validationDefault(Validator $validator): Validator
    {
...
		$this->loadModel('Types');
		$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'),
...
		return $validator;
    }

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!

If both fields are in the same record, then I think the $context variable passed to the rule will have everything you need.

Both fields are in the same table; I’m using Cakephp 3.4;
How would I formulate using $context ?

Ah, that’s so much simpler, I was reading it was like a reference to a set of values in another table.

This is from Cake 4 so hopefully its similar to 3 - if not you’ll have to wait for an experienced person to post!

Still in src/Model/Table/MembresTable.php

        public function validationDefault(Validator $validator): Validator
        {
...
                $validator
                        ->add('nb_annee_adhesion', 'no-misspelling', [
                                'rule' => ['compareWith', 'type_id'],
                                'message' => __('Types are not equal')
                                ]);

...
                return $validator;
        }

Maybe I did not explain properly the situation ,

Here really what I want in “MembresTable.php”.

Put a function there that dumps the contents of $context, or put an xdebug breakpoint there and examine the value. It should be pretty obvious from that point.

https://book.cakephp.org/3/en/core-libraries/validation.html#using-custom-validation-rules

$validator->add('title', 'custom', [
    'rule' => function ($value, $context) use ($extra) {
        // Custom logic that returns true/false
    },
    'message' => 'The title is not valid'
]);

insert into body

debug($context); die;

and you will see what you get.