Welcome Guest, Not a member yet? Register   Sign In
how to do unique validation, CodeIgniter2 + Doctrine2
#1

[eluser]xjermx[/eluser]
I am working with CodeIgniter2.1.0/Doctrine2.1.1

I have a registration form, and I want to check to be sure that any email address entered is unique.

I have tried this:
Code:
$this->form_validation->set_rules('email', 'E-mail',
                 'required|valid_email|is_unique[users.email]');

Which returns:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Signup_form::$db

Filename: libraries/Form_validation.php

Line Number: 954


Fatal error: Call to a member function limit() on a non-object in /../systemFolder/libraries/Form_validation.php on line 954

I have also tried following the example on the form_validation page in the user guide for CI:

Code:
$this->form_validation->set_rules('email', 'E-mail', 'callback_email_check');
(along with an email_check function, basically copy/pasted from the example in the user guide)

This gives me:

Quote:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'email_index'' in /../applicationFolder/libraries/Doctrine/DBAL/Statement.php:131 Stack trace: #0 /../applicationFolder/libraries/Doctrine/DBAL/Statement.php(131): PDOStatement->execute(NULL) #1 /../applicationFolder/libraries/Doctrine/ORM/Persisters/BasicEntityPersister.php(239): Doctrine\DBAL\Statement->execute() #2 /../applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php(896): Doctrine\ORM\Persisters\BasicEntityPersister->executeInserts() #3 /../applicationFolder/libraries/Doctrine/ORM/UnitOfWork.php(304): Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata in /../applicationFolder/libraries/Doctrine/DBAL/Statement.php on line 131

What am I doing wrong? Are either of these 'correct'? Is there just something I'm missing to make one or both work? Is there a better way to go about this?
#2

[eluser]xjermx[/eluser]
Well I've found a solution. I'm not sure if its the best or most elegant, but it works.

Code:
$this->form_validation->set_rules('email', 'E-mail',
                 'required|valid_email|callback_email_check');

Code:
public function email_check($str)
{
$user = $this->doctrine->em->getRepository('Entities\User')->findOneBy(array('email' => $str));
    if (isset($user)) {
            $this->form_validation->set_message('email_check', 'This email address is already in use');
                    return FALSE;
    } else
    {
        return TRUE;
    }
}

Still open to anyone offering opinions on how this might be done better!




Theme © iAndrew 2016 - Forum software by © MyBB