I am using Doctrine 2.2.1 and CodeIgniter 2.1.0
I am attempting to do unique validation on an email address field from a form - simply to make sure that the field does not already exist in the database.
I started by checking this good (but outdated) tutorial here and the CodeIgniter documentation here
The CI documentation shows examples using is_unique[table.field] and also seems to say that using a callback can do this but I don't seem to be grasping how that code does it, and I cannot make it work myself.
I have tried this:
$this->form_validation->set_rules('email', 'E-mail',
'required|valid_email|is_unique[users.email]');
and this:
$this->form_validation->set_rules('email', 'E-mail', 'callback_email_check');
(which is cribbed almost verbatim from the CodeIgniter example)
Neither work. The first gives me the following output:
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
The second gives me this output:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'email@address.com' 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
Am I overlooking something simple? Any help with how to correctly do a unique validation against the DB with CI/Doctrine? Thanks!
Note: I was able to get $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email|is_unique[users.email]');
working after adding 'database' to the autoload config, however, this breaks Doctrine entirely.
This seems to solve the problem. Not sure if it is the best or most elegant way, but it works.
$this->form_validation->set_rules('email', 'E-mail',
'required|valid_email|callback_email_check');
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;
}
}
I just faced this same problem, it was an issue with CodeIgniter 2.1.
There are two fixes for it.
1. Upgrade CodeIgniter from the repo source, it has the fix for it and many other fixes for other problems:
Repo Source at Github
or
2. Open system/libraries/Form_validation
Go to line 954
and replace the function is_unique()
with:
public function is_unique($str, $field)
{
list($table, $field) = explode('.', $field);
if (isset($this->CI->db)) {
$query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
return $query->num_rows() === 0;
}
return FALSE;
}
This should fix it.
here is 100% working solution:
->go to system/libraries/form_validation.php
->go to line no 954 (your may be arround 950s)
->Replace is_unique function with following code
public function is_unique($str, $field)
{
list($table, $field)=explode('.', $field);
$this->CI->load->database('default');
$query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
return $query->num_rows() === 0;
}
Actually Error was because of unknown $db object because there was no database loaded before calling $db.
As for me solution is in the table name "User", in your validation rule it must be : $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email|is_unique[Users.email]');
- table name is uppercased.
I hope this helps someone.
Also in CodeIgniter 2.0 you should use:
is_unique[db,table_name,field_name]
Just add $this->load->database();
in the _construct
function