Need help with validation in cakephp3


I need help with creating a validation rule for the following scenario:



Table “containers” has a uniqueness constraint on (site_id, name) to ensure that there is, for example, only one container named “beets” at any site with the same name. This works as expected. In the MySQL client, when I try to create a second container named beets at a site, I get the expected constraint violation error.

This works in CakePHP too, but I get an ugly error message. I looked in the ContainersTable.php class, and there here is what I see for the validation code:

namespace App\Model\Table;

use App\Model\Entity\Container;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

 * Containers Model
 * @property \Cake\ORM\Association\BelongsTo $Sites
 * @property \Cake\ORM\Association\BelongsToMany $Groups
class ContainersTable extends Table

     * Initialize method
     * @param array $config The configuration for the Table.
     * @return void
    public function initialize(array $config)



        $this->belongsTo('Sites', [
            'foreignKey' => 'site_id'
        $this->belongsToMany('Groups', [
            'foreignKey' => 'container_id',
            'targetForeignKey' => 'group_id',
            'joinTable' => 'containers_groups'

     * Default validation rules.
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
    public function validationDefault(Validator $validator)
            ->allowEmpty('id', 'create');



        return $validator;

     * Returns a rules checker object that will be used for validating
     * application integrity.
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
    public function buildRules(RulesChecker $rules)
        $rules->add($rules->existsIn(['site_id'], 'Sites'));
        return $rules;

Any ideas on how to write the code to ensure that we don’t have multiple “beets” container objects at “Grocery Store” site?

@Graziel, any ideas on this? :smiley:


My solution, at this point, is just to perform an “exists()” call on the table object:

$container = $this->Containers->patchEntity($containers, $this->request->data);
if ( $this->Containers->exists([ 'name' => $container->name, 'site_id' => $container->site_id ]) ) {
    $this->Flash->error("Container with that name already exists at this site");
} else {
    // Perform normal save operation.

Similar code exists in the ‘edit’ method.

I would prefer to handle this in the proper place: the validation rules for model table class.


sounds like


That’s PERFECT!!!

I’m not sure how I missed that in the docs.

Thanks, again.