Posts Tagged ‘Code snippet to check if form typed email is present in Database with Zend Framework / substitute for Db_NoRecordExists (Db_RecordExists)’

Code snippet to check if form typed email is present in Database with Zend Framework / substitute for Db_NoRecordExists (Db_RecordExists)

Friday, August 27th, 2010

Reading Time: 2minutes
I’m writting a registration form and in Zend Framework (ZF) and I came to the point where the input email form address had to be stored in database if it’s not already there.
Thus I needed a way to complete the task. Firstly I tried using Db_NoRecordExists and Db_RecordExists which is the “proper” way to complete this task in Zend Framework.

I tried to apply a code to my Zend Form similar to:

$email = new Zend_Form_SubForm();
$email_addr = new Zend_Form_Element_Text('email'),
->setValue('email (Gmail, Yahoo, etc.)')
->addValidator('EmailAddress', false)
->addValidator('Db_NoRecordExists', false, array('table' => 'my_table_name','field' => 'email'))
->setAttribs(array('minlength' => '5', 'maxlength' => '40'));

Nevertheless I couldn’t make the above code to work out, thus I eventually ended up writting my own function in order to check if the form input email is not already stored in database and if it’s stored to render again my Zend Registration input form.

Herein I present the code with a hope that somebody else that is a juniour with PHP Zend Framework just I am will benefit out of it.
1. First here is what is in my form controller application/controller/RegistrationForm.php:

// pick up the email entered earlier from Zend Form
$this->email_address = $email_address = $formData['email']['email'];

// if query_result = 1 then registrar has already an existant email in db
$check_if_email_in_db = new Model_DbTable_Registration();
$query_result = $check_if_email_in_db->checkUserisNotRegistered($this->email_address);

if($query_result == ‘1’) {
$this-view->errorMsg =’ Your email address is already taken! Please type another one. ‘;

In order to be able to use the above code you will also have to have existing application/models/DbTable/Registration.php or whatever name you’d like to choose.
2. Within that file place code like:

class Model_DbTable_Registration extends Zend_Db_Table_Abstract
// set database name
protected $_name = 'database.my_table_name';
public function checkUserisNotRegistered($email_address) {

throw new Exception(’empty parameter passed.’);

$where = $this->_db->quoteIdentifier(’email’) . ‘ = “‘.$email_address.'”‘;

$result = $this->fetchAll($this->select()
$query_result = $result[‘0′][’email’];

if($query_result == NULL) {

$query_result = ‘0’;

} else {
$query_result = ‘1’;

return $query_result;


Of course change the database.my_table_name with whatever your database and table name is, also assure $email_address which is defined by the above code $this->email_address = $email_address = $formData[’email’][’email’]; has to be properly tuned in your application/controller/RegistrationController.php or whatever it is called.In my case I use the $formData data array to store my email variable earlier taken by the Zend Form I use the following code to check if my request is properly posted by the Zend_Form:

if ($this->getRequest()->isPost()) {
$formData = $this->_request->getPost();

As you can see I use $formData to get Zend_Form posted conteny, if you use another modify the above code to math up with it.
Hope that’s helpful to somebody out there!
Feedback as always is very welcome.