title: Stopping product saving process in observer
body: <p>I am currently developing a module working with the product edit in the backend.
  Its purpose is to retrieve categories the product belongs to and populate an attribute (the Brand attribute) with the list of selected categories.</p>
  
  <p><strong>It is mandatory for the admin to select at least one category.</strong></p>
  
  <p>My module works as expected except that I don't know how to stop the saving process if the admin hasn't selected any category while editing a product.</p>
  
  <p>Here is the workflow</p>
  
  <ul>
  <li>Administrator selects categories in the category tab in the product edit page</li>
  <li>Admin clicks on "Save"</li>
  <li>My module "observes" and gathers all categories</li>
  </ul>
  
  <p>--> If there are selected categories</p>
  
  <ul>
  <li>My module's observer does its stuff to update the Brand attribute</li>
  </ul>
  
  <p>--> Else</p>
  
  <ul>
  <li>My module's observer adds an error to the admin session</li>
  <li>My module's observer should tell Magento to stop saving the product. But how do I do that ?</li>
  </ul>
  
  <p>The generic question would maybe be : how to pass a "stop save" argument to an observer ?</p>
  
  <p>Here are a sample of my config.xml file and the method that deals with the workflow I explained above.</p>
  
  <p>Thanks a lot for your help and have fun Magentoing !</p>
  
  <p><strong>config.xml</strong></p>
  
  <pre><code>    &lt;catalog_product_prepare_save&gt;
          &lt;observers&gt;
              &lt;brands_product_save_observer&gt;
                  &lt;type&gt;singleton&lt;/type&gt;
                  &lt;class&gt;brands/observer&lt;/class&gt;
                  &lt;method&gt;saveProductBrand&lt;/method&gt;
              &lt;/brands_product_save_observer&gt;
          &lt;/observers&gt;
      &lt;/catalog_product_prepare_save&gt;
  </code></pre>
  
  <p><strong>Observer.php</strong></p>
  
  <pre><code>public function saveProductBrand($observer) {
      $product = $observer-&gt;getProduct();
      $categoryIds = $product-&gt;getCategoryIds();
      if (isset($categoryIds)) {
          foreach ($categoryIds as $categoryId) {
              $isBrandCategory = Mage::getModel('brands/navigation')-&gt;isBrandCategory($categoryId);
              if ($isBrandCategory)
                  $brandCategories[] = $categoryId;
          }
          if (isset($brandCategories)) {
              $brandId = Mage::getModel('brands/navigation')-&gt;getBrand($brandCategories[0]);
              if ($brandId) {
                  $attribute = Mage::getModel('eav/config')-&gt;getAttribute('catalog_product', 140);
                  foreach ($attribute-&gt;getSource()-&gt;getAllOptions(true, true) as $option) {
                      $attributeArray[$option['label']] = $option['value'];
                  }
                  $categoryName = Mage::getModel('catalog/category')-&gt;load($brandId)-&gt;getName();
                  $product-&gt;setData('brand', $attributeArray[$categoryName]);
              }
          } else {
              Mage::getSingleton('adminhtml/session')-&gt;addError(Mage::helper('catalog')-&gt;__('Please add this product to a brand in the "Categories" tab.'));
  
              HERE SOME CODE TO TELL MAGENTO TO STOP SAVING THE PRODUCT
  
              return;
          }
      }
  }
  </code></pre>