# Validate unique two tables

**URL:** <https://discourse.cakephp.org/t/validate-unique-two-tables/10098>\
**Category:** Need Help\
**Created:** [January 17, 2022, 9:15pm UTC](https://discourse.cakephp.org/t/validate-unique-two-tables/10098 "2022-01-17T21:15:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [January 17, 2022, 9:15pm UTC](https://discourse.cakephp.org/t/validate-unique-two-tables/10098/1 "2022-01-17T21:15:12Z")

</div>

Table:

```auto
Navigations
column: id
column: site_id

```

Table:

```auto
NavigationsTexts
column: id
column: navigation_id
column: language_id
column: slug

```

Navigations table column: `site_id` allow `NULL, 1, 2`

I need to do a unique validation for two table. When I save, my records are saved in both tables at the same time.

Unique insert: (`site_id` = table `Navigations`), (`language_id, slug` = table `NavigationsTexts`)

Is it possible to validate between two tables?

---

<div class="post-metadata">

**Author:** ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)\
**Post date:** [January 18, 2022, 10:45am UTC](https://discourse.cakephp.org/t/validate-unique-two-tables/10098/2 "2022-01-18T10:45:06Z")

</div>

you will need to do a custom validation. See [https://book.cakephp.org/4/en/core-libraries/validation.html#using-custom-validation-rules](https://book.cakephp.org/4/en/core-libraries/validation.html#using-custom-validation-rules)

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [January 18, 2022, 6:56pm UTC](https://discourse.cakephp.org/t/validate-unique-two-tables/10098/3 "2022-01-18T18:56:29Z")

</div>

Please would not be an example, it is not entirely clear to me from the documentation.

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [January 18, 2022, 10:47pm UTC](https://discourse.cakephp.org/t/validate-unique-two-tables/10098/4 "2022-01-18T22:47:37Z")

</div>

Before sample code would be presented, it would be best to understand what you’re trying to do.

How are you currently posting 2 records into 2 tables at the same time? Wouldn’t `Navigations` be posted first? Otherwise how could you get the value for `navigation_id` in the `NavigationsTexts` table?

Would it be more correct to assume the Navigations record already exists, and its the validation of the navigation\_id field in the NavigationsTexts table which requires the validation?

Please explain the situation more clearly, what does _ **at the same time** _ means to you.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 19, 2022, 1:39am UTC](https://discourse.cakephp.org/t/validate-unique-two-tables/10098/5 "2022-01-19T01:39:44Z")

</div>

> [@Jawfin](#):
>
> How are you currently posting 2 records into 2 tables at the same time? Wouldn’t `Navigations` be posted first? Otherwise how could you get the value for `navigation_id` in the `NavigationsTexts` table?

Form helper plus patching does this just fine with field names like `navigations_texts[0].language_id`.

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [January 19, 2022, 7:06am UTC](https://discourse.cakephp.org/t/validate-unique-two-tables/10098/6 "2022-01-19T07:06:06Z")

</div>

## Form:

```auto
site_id = 1
navigations_texts[0][language_id] = 1
navigations_texts[0][slug] = '/some-url'

```

after click save…

```auto
$Content = $this->Navigations->newEmptyEntity();
$Content = $this->Navigations->patchEntity($Content, $this->request->getData());
$this->Navigations->save($Content)

```

A new autoincrement ID is created above, in the second table, the new id is copied to `navigation_id`.  
Validation is performed during patchentity and here I would need to verify that the same string no longer exists in the slug column if `site_id = 1` is in table 1.

## Example

Table: Navigations

```auto
id, site_id
1, 1

```

Table: NavigationsTexts

```auto
id, navigation_id, language_id, slug
1, 1, 1, '/some-url'

```

If I enter the same URL with `site_id = 1`, it will throw me an error, because the record already exists. If I choose `site_id = 2`, the record can be saved.
