# Need help with validation in cakephp3

**URL:** <https://discourse.cakephp.org/t/need-help-with-validation-in-cakephp3/2816>\
**Category:** Need Help\
**Created:** [July 2, 2017, 10:29pm UTC](https://discourse.cakephp.org/t/need-help-with-validation-in-cakephp3/2816 "2017-07-02T22:29:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![caiblack](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/caiblack/32/202_2.png) [@caiblack](https://discourse.cakephp.org/u/caiblack)\
**Post date:** [July 2, 2017, 10:29pm UTC](https://discourse.cakephp.org/t/need-help-with-validation-in-cakephp3/2816/1 "2017-07-02T22:29:02Z")

</div>

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

```
sites
+id
+name

containers
+id
+site_id
+name

```

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:

```
<?php
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)
    {
        parent::initialize($config);

        $this->table('containers');
        $this->displayField('name');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $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)
    {
        $validator
            ->uuid('id')
            ->allowEmpty('id', 'create');

        $validator
            ->requirePresence('site_id')
            ->notEmpty('site_id')
            ->uuid('id');

        $validator
            ->requirePresence('name')
            ->notEmpty('name');

        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? 😃

---

<div class="post-metadata">

**Author:** ![caiblack](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/caiblack/32/202_2.png) [@caiblack](https://discourse.cakephp.org/u/caiblack)\
**Post date:** [July 2, 2017, 10:36pm UTC](https://discourse.cakephp.org/t/need-help-with-validation-in-cakephp3/2816/2 "2017-07-02T22:36:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [July 3, 2017, 7:20pm UTC](https://discourse.cakephp.org/t/need-help-with-validation-in-cakephp3/2816/3 "2017-07-03T19:20:17Z")

</div>

sounds like [https://book.cakephp.org/3.0/en/orm/validation.html#creating-unique-field-rules](https://book.cakephp.org/3.0/en/orm/validation.html#creating-unique-field-rules)

---

<div class="post-metadata">

**Author:** ![caiblack](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/caiblack/32/202_2.png) [@caiblack](https://discourse.cakephp.org/u/caiblack)\
**Post date:** [July 3, 2017, 8:02pm UTC](https://discourse.cakephp.org/t/need-help-with-validation-in-cakephp3/2816/4 "2017-07-03T20:02:04Z")

</div>

That’s PERFECT!!!

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

Thanks, again.
