How to avoid duplicate emails in the database?

Asked

Viewed 451 times

3

I’m creating a simple CMS in Cakephp to learn how to use the framework. Within my CMS, in the administration part of the site, there is a tab that is called email and in this tab we see all the emails that were registered by the administrator. However, when registering an email it allows us to register the same email over and over again. How do I eliminate this failure?

  • 1

    I don’t know Cake, but the validation rule should be like most FW, look for the attribute unique, or do an email check in the controller before registering. Here I found some answers that might help: http://stackoverflow.com/questions/2461267/cakephp-isunique-for-2-fields

2 answers

2

Just set 2 rules in the email field.

Inside your model, which has this field, is this way:

public $validate = array(
// Aqui vai o nome do campo
'email' => array(
    // O nome que você quiser dar na validação do campo
    'rule1Email' => array(
        // Tipo de regra
        // parãmetro true verifica se o host é válido
        'rule' => array('email', true),
        //Sua mensagem de erro para esta regra
        'message' => 'Insira um email válido',
        //Obriga a preencher
        'required' => true,
        //Se quer limitar a apenas alguma action
        'on' => 'create'
    ),
    //Outra regra para o mesmo campo.
    'rule2isUnique' => array(
        //Tipo de regra
        'rule' => 'isUnique',
        //Sua mensagem de erro para esta regra
        'message' => 'Email já cadastrado'
        )
    )
);

Font and other options:http://book.cakephp.org/2.0/en/models/data-validation.html

0

just put the field where the email is registered as UNIQUE, if an Insert tries to enter an equal value it will not allow.

ALTER TABLE `test`.`aplicacao` 
CHANGE COLUMN `name` `email` VARCHAR(255) NOT NULL ,
ADD UNIQUE INDEX `email_UNIQUE` (`email` ASC);

if an Insert tries to register a value that already exists it will return error as below

Error Code: 1062. Duplicate entry '[email protected]' for key 'email_UNIQUE'

so you guarantee the bank, the application is already another subject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.