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: -
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!
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.
$validator->add('title', 'custom', [
'rule' => function ($value, $context) use ($extra) {
// Custom logic that returns true/false
},
'message' => 'The title is not valid'
]);