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

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.

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

Thats what application rules are for: Validating Data - 4.x

See Validating Data - 4.x

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


r

with

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

you shouild already get what you want.

The code

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

inside your validator is not needed.

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


Actually Mysql developer has set unique email constraint therefore I got this error when same error

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

try this:-

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

1 Like

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