title: Unique email check in zend framework is not working properly
body: <p>I am working on a project based on Zend framework. In user registration I have to check for unique email. My code is working fine when I register a user for the first time, but when I try to update the user information and press the update button, it gives the error message:</p>
  
  <pre><code>Email already taken 
  </code></pre>
  
  <p>Please help me to solve this problem. My code is attached below:</p>
  
  <pre><code>$this-&gt;addElement('text', 'email', array(
                  'label'      =&gt; 'Your email address:',
                  'required'   =&gt; true,
                  'filters'    =&gt; array('StringTrim'),
                  'validators' =&gt; array(
                          'EmailAddress',
                          array('Db_NoRecordExists', true, array(
                                  'table' =&gt; 'users',
                                  'field' =&gt; 'email',
                                  'messages' =&gt; array(
                                          'recordFound' =&gt; 'Email already taken'
                                  )
                          )
                          )
                  )
          ));
  </code></pre>
  
  <p>I have changed my controller to this:</p>
  
  <pre><code>public function addAction()
      {
          $modelUsers = new Model_Users();
          $userId = $this-&gt;_getParam('userId');
          $form = $this-&gt;_getAddForm();
  
          if ($userId) {              
              $populateData = array();
  
              $user = $modelUsers-&gt;fetch($userId);
  
              if ($user instanceof Model_User) {
                  $populateData = $user-&gt;toArray();
              }
  
              $form-&gt;populate($populateData);
          }
  
          $request = $this-&gt;getRequest();
  
          if ($request-&gt;isPost()) {
              $email = $this-&gt;getRequest()-&gt;getParam('email');
              if (strtolower(trim($email)) == $modelUsers-&gt;fetchByEmail($email)) {
                  // change $this-&gt;_user-&gt;getAccount()-&gt;getEmail() to retrieve the user's current email address
  
                  // remove validator from form
                  $form-&gt;getElement('email')-&gt;removeValidator('Db_NoRecordExists');
              }
  
              $post = $request-&gt;getPost();
  
              if ($form-&gt;isValid($post)) {
                  $values = $form-&gt;getValidValues($post);
                  $data = array(
                      'firstName'     =&gt; $values['firstName'],
                      'userTypeId'    =&gt; 2,
                      'lastName'      =&gt; $values['lastName'],
                      'email'         =&gt; $values['email'],
                      'userName'      =&gt; $values['userName'],
                      'password'      =&gt; $values['password'],
                      'role'          =&gt; $values['role']
                  );
  
                  if ($userId) {
                      $user = $modelUsers-&gt;fetch($userId);
                      if ($user instanceof Model_User) {
                          $user-&gt;setFromArray($data);
                          $success = $user-&gt;save();
                          if ($success) {
                              echo Zend_Json::encode(array('status' =&gt; self::STATUS_SUCCESS, 'message' =&gt; 'Successfully updated the user!'));
                              exit;
                          }
                      }
                  } else {
                      $user = $modelUsers-&gt;createRow($data);
                      $success = $user-&gt;save();
                      if ($success) {
                          echo Zend_Json::encode(array('status' =&gt; self::STATUS_SUCCESS, 'message' =&gt; 'Successfully added the user!'));
                          exit;
                      }
  
                  }
                  echo Zend_Json::encode(array('status' =&gt; self::STATUS_FAILED, 'message' =&gt; 'user not added'));
                  exit;               
              } else {
  
                  $errors = array();
                  $errors = $form-&gt;errors();              
  
                  echo Zend_Json::encode(array('status' =&gt; self::STATUS_ERROR, 'data' =&gt; $errors));
                  exit;
  
              }
          }
  
          $this-&gt;view-&gt;form = $form;
          $this-&gt;_helper-&gt;layout-&gt;disableLayout();
  
      }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>public function fetchByEmail($email)
  {
      $email=fetchOne('SELECT email FROM users WHERE email = $email');
      //$select-&gt;where('email=?',$email) ;
      //$student = $this-&gt;fetchRow($select);
      return $email;
  }
  </code></pre>
  
  <p>But still this is not working</p>