title: Magento - check if customer attribute already exists
body: <p>I have a custom module which is called on the <code>customer_save_observer_executed</code> event and specifically when a customer updates their details in their account page (name, password, email etc) I have added a custom attribute called display_name. </p>
  
  <p>When a customer submits this form i need to check if the display_name currently exists for any other customer. If it doesn't then <code>setDisplayName(...)</code> else do nothing/display error message.</p>
  
  <p>I was hoping to do this with this snippet:</p>
  
  <pre><code>$customer_check = Mage::getModel('customer/customer')
  -&gt;getCollection()
  -&gt;addAttributeToSelect('display_name')
  -&gt;addAttributeToFilter('display_name',$new_name)-&gt;load();
  
  if ( is_object($customer_check) &amp;&amp; count($customer_check) &gt;= 2) {
      // dont allow - duplicate customer displayname
  }
  else {
      // allow update....
  }
  </code></pre>
  
  <p>My current code in Model -> Observer.php</p>
  
  <pre><code>class xxxxxxx_Model_Observer
  {
      public function xxxxxxxx(Varien_Event_Observer $observer)
      {
          // to stop event being fired twice
          if(Mage::registry('customer_save_observer_executed')){
              return $this;
          }
  
          $postData = Mage::app()-&gt;getRequest()-&gt;getPost();
          $customer = $observer-&gt;getCustomer();
  
          // if updating NOT a new customer
          if($customer instanceof Mage_Customer_Model_Customer &amp;&amp; !$customer-&gt;isObjectNew()) {
  
              // check display name is posted
              if(!empty($postData['display_name'])){
  
                  $current_name = $customer-&gt;getDisplayName();
                  $new_name = $postData['display_name'];
  
                  // duplicate check
                  $customer_check = Mage::getModel('customer/customer')
                  -&gt;getCollection()
                  -&gt;addAttributeToSelect('display_name')
                  -&gt;addAttributeToFilter('display_name',$new_name)-&gt;load();
  
                  if ( is_object($customer_check) &amp;&amp; count($customer_check) &gt;= 2) {
                      // dont allow - duplicate customer displayname
                  }
                  else {
  
                      if( $postData['display_name'] !== $current_name ) {
                          $customer-&gt;setDisplayName($postData['display_name']);
                      }
  
                  }
  
              }
  
          }
  
          Mage::register('customer_save_observer_executed',true); 
  
      }
  }
  </code></pre>
  
  <p>but this just updates the display_name even if i deliberately set it to a duplicate of another customers</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Looking into this further it looks like the module function itself is not being run or nothing inside it is taking affect, as whatever i put in it doesn't work. It is infact the default behaviour that is setting the displayname not my module. My module is activated and my config file uses the <code>customer_save_commit_after</code> event as shown below:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;config&gt;
      &lt;modules&gt;
          &lt;xxx_xxxxx&gt;
              &lt;version&gt;0.0.1&lt;/version&gt;
          &lt;/xxx_xxxxx&gt;
      &lt;/modules&gt;
      &lt;global&gt;
          &lt;models&gt;
              &lt;xxx_xxxxx&gt;
                  &lt;class&gt;xxx_xxxxx_Model&lt;/class&gt;
              &lt;/xxx_xxxxx&gt;
          &lt;/models&gt;
          &lt;events&gt;
              &lt;customer_save_commit_after&gt;
                  &lt;observers&gt;
                      &lt;xxx_xxxxx&gt;
                          &lt;class&gt;xxx_xxxxx/observer&lt;/class&gt;
                          &lt;method&gt;xxxxxx&lt;/method&gt;
                          &lt;type&gt;singleton&lt;/type&gt;
                      &lt;/xxx_xxxxx&gt;
                  &lt;/observers&gt;
              &lt;/customer_save_commit_after&gt;
          &lt;/events&gt;
      &lt;/global&gt;
  &lt;/config&gt;
  </code></pre>