# Rules from two tables

**URL:** <https://discourse.cakephp.org/t/rules-from-two-tables/8073>\
**Category:** Need Help\
**Created:** [July 17, 2020, 2:34am UTC](https://discourse.cakephp.org/t/rules-from-two-tables/8073 "2020-07-17T02:34:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [July 17, 2020, 2:34am UTC](https://discourse.cakephp.org/t/rules-from-two-tables/8073/1 "2020-07-17T02:34:23Z")

</div>

I want to apply isUnique from two table class.

$rules-\>add($rules-\>isUnique(  
[‘person\_id’],  
‘This person Id Number already exists.’  
));

The above code checks is unique for single table class in which it is defined.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [July 17, 2020, 4:15am UTC](https://discourse.cakephp.org/t/rules-from-two-tables/8073/2 "2020-07-17T04:15:07Z")

</div>

is this still your [Generic Custom Validation](https://discourse.cakephp.org/t/generic-custom-validaton-page/8061/11) question?

---

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [July 17, 2020, 4:45am UTC](https://discourse.cakephp.org/t/rules-from-two-tables/8073/3 "2020-07-17T04:45:14Z")

</div>

Nopes.

Its another one.  
And I will share the generic validation updates on [Generic Custom Validaton Page](https://discourse.cakephp.org/t/generic-custom-validaton-page/8061/11) node.

---

<div class="post-metadata">

**Author:** ![bizdev](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bizdev/32/1951_2.png) [@bizdev](https://discourse.cakephp.org/u/bizdev)\
**Post date:** [July 17, 2020, 5:02am UTC](https://discourse.cakephp.org/t/rules-from-two-tables/8073/4 "2020-07-17T05:02:13Z")

</div>

We assume that both tables are related then you can use something like:

```php
$rules->add($rules->isUnique(['person_id', 'group_id']));

```

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

If it’s not related you have to use [custom validator](https://book.cakephp.org/3/en/core-libraries/validation.html#creating-validators) to do the job.

---

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [July 17, 2020, 10:30am UTC](https://discourse.cakephp.org/t/rules-from-two-tables/8073/5 "2020-07-17T10:30:25Z")

</div>

> [@bizdev](#):
>
> `group_id`

I wanted something like this  
$rules-\>add($rules-\>isUnique([‘Table1.person\_id’, ‘Table2.person\_id’]));

Person\_id is should be unique in either of the table.
