I have a HABTM relation like : Post <-> Tag
(a Post can have multiple Tag, and same the other way).
This work with the multiple checkbox selection generated by Cakephp. But I want to have at least one Tag for every Post and throw an error if someone try to insert an orphan.
I'm looking for the cleanest/most CakePHP alike way to do this.
This is more or less an update of this HABTM form validation in CakePHP question, as I get the same problem on my cakephp 2.7 (last cakephp 2.x for now with php 5.3 support at the date of 2016) and can't find a good way to do it.
Here are what I think is the best for now. It use the cakephp 3.x behaviour for HABTM validation.
I choose to only work in model, with the most generic code.
In your
AppModel.php
, set thisbeforeValidate()
andafterValidate()
After this, you can use your validation in you model like this :
This answer use :