# SQLSTATE\[23000\]: Integrity constraint violation: 1062 Duplicate entry

**URL:** <https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259>\
**Category:** Uncategorized\
**Created:** [June 9, 2023, 6:23am UTC](https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259 "2023-06-09T06:23:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![daryl](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/daryl/32/2735_2.png) [@daryl](https://discourse.cakephp.org/u/daryl)\
**Post date:** [June 9, 2023, 6:23am UTC](https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259/1 "2023-06-09T06:23:00Z")

</div>

Hey guys. how to handle this erro SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry  
actually this constraint set in the mysql side want to show this error message in bottom of fields but cakephp throw this error whole screen… please tell me how to handle this error validation in well form.

 ![Screenshot from 2023-06-09 11-49-30](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/1/174b2216f61fc147d3dccd69dd3944e4b8eb9d1e.png)

i wan this return me as text form message like ‘this is E-mail already in used’ please help me guys

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [June 9, 2023, 9:50am UTC](https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259/2 "2023-06-09T09:50:47Z")

</div>

Thats what application rules are for: [Validating Data - 4.x](https://book.cakephp.org/4/en/orm/validation.html#applying-application-rules)

See [Validating Data - 4.x](https://book.cakephp.org/4/en/orm/validation.html#creating-unique-field-rules)

---

<div class="post-metadata">

**Author:** ![daryl](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/daryl/32/2735_2.png) [@daryl](https://discourse.cakephp.org/u/daryl)\
**Post date:** [June 9, 2023, 10:57am UTC](https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259/3 "2023-06-09T10:57:16Z")

</div>

thank for reply @KevinPfeifer and i had already read docs but stack with same error…

my mode side code:-

$validator  
-\>email(‘email’)  
-\>requirePresence(‘email’, ‘create’)  
-\>notEmptyString(‘email’)  
-\>add(‘email’, ‘unique’, [‘message’ =\> ‘this Email Already in Use’, ‘rule’ =\> ‘validateUnique’, ‘provider’ =\> ‘table’]);

public function buildRules(RulesChecker $rules): RulesChecker  
{  
$rules-\>add($rules-\>isUnique([‘email’]), [‘errorField’ =\> ‘email’]);  
return $rules;  
}

please help me, let me show you image what i want, i want to show error near the Email field in form

 ![Screenshot from 2023-06-09 16-26-11](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/8/837f60b159783a90a9629aad63611b859dad9a6d.png)  
r

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [June 9, 2023, 11:14am UTC](https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259/4 "2023-06-09T11:14:30Z")

</div>

with

```auto
public function buildRules(RulesChecker $rules): RulesChecker
{
    $rules->add($rules->isUnique(['email']), ['errorField' => 'email']);
    return $rules;
}

```

you shouild already get what you want.

The code

```auto
->add('email', 'unique', [...]);

```

inside your validator is not needed.

---

<div class="post-metadata">

**Author:** ![daryl](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/daryl/32/2735_2.png) [@daryl](https://discourse.cakephp.org/u/daryl)\
**Post date:** [June 9, 2023, 12:51pm UTC](https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259/6 "2023-06-09T12:51:28Z")

</div>

yes @KevinPfeifer i have already done it but I am still getting same error… SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘xyz@gmail.com’ for key ‘email’.

this is another app and it work fine but i am working with different project

 ![Screenshot from 2023-06-09 16-26-11](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/8/837f60b159783a90a9629aad63611b859dad9a6d.png)  
Actually Mysql developer has set **unique email constraint** therefore I got this error when same error

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [June 9, 2023, 12:56pm UTC](https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259/7 "2023-06-09T12:56:37Z")

</div>

First of all tell me that… are you using any plugin in your project? if yes you have plugin then you have to do some configuration before save or anything else you want to do. so please make you are you using plugin or not after that i will help you @daryl .  
because you already define validation rules even @KevinPfeifer already suggest you perfect code and solution with example so it would be work

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [June 9, 2023, 4:52pm UTC](https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259/8 "2023-06-09T16:52:04Z")

</div>

try this:-

$validator  
-\>email(‘email’)  
-\>requirePresence(‘email’, ‘create’)  
-\>notEmptyString(‘email’)  
-\>add(‘email’, ‘unique’, [‘message’ =\> ‘this Email Already in Use’, ‘rule’ =\> ‘validateUnique’, ‘provider’ =\> ‘table’])

---

<div class="post-metadata">

**Author:** ![Kaiser85](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kaiser85/32/2714_2.png) [@Kaiser85](https://discourse.cakephp.org/u/Kaiser85)\
**Post date:** [June 12, 2023, 2:06pm UTC](https://discourse.cakephp.org/t/sqlstate-23000-integrity-constraint-violation-1062-duplicate-entry/11259/9 "2023-06-12T14:06:47Z")

</div>

I’ve also tested Kevin’s code and it works perfectly. You should try what @shaan007 is advicing you and let us know.
