View Single Post
  #4 (permalink)  
Old 05-14-2007, 11:34 PM
frosty1 frosty1 is offline
Junior Member
 
Join Date: May 2007
Posts: 25
Default error handling

i use this very simple helper to handle my errors. it saves the errors to a session so you dont have to worry about redirects clearing the error.

PHP Code:
<?php
class libErrors
{
    private 
$ns;
    private 
$errors;
    
    function 
__construct()
    {
        
$this->ns = new Zend_Session_Namespace('errors'); 
        
$this->errors $this->ns->errors;   
    }
    
    function 
clear()
    {
        unset(
$this->errors);
        
$this->updateNs();
    }
    
    function 
add($error)
    {
        
$this->errors[] = $error;
        
$this->updateNs();
    }
    
    function 
hasErrors()
    {
        if(
count($this->errors) > 0){
            return 
true;
        }
    }
    
    function 
get()
    {
        return 
$this->errors;
    }
    
    private function 
updateNs()
    {
        
$this->ns->errors $this->errors;
    }
}
i then use this view helper, renderAlert (which handles both errors and messages to the user)

PHP Code:
    public function RenderAlert(){
        
$m = new libMessage();
        
$e = new libErrors();
        
$alert false;
        
$view Zend_Registry::get('view');
        
        if(
$m->hasMessage() || $e->hasErrors()){
            if(
$m->hasMessage()){
                
$message "<p>" $m->get() . "</p>";
            }
            
            if(
$e->hasErrors()){
                
$error "<p>The following errors have occurred:</p>" $view->HtmlList($e->get());
            }
            
$alert "<div class='message_box'>" $message $error "</div>";
        }
        
        
//after this renders it clears the errors and messages
        
$m->clear();
        
$e->clear();
        
        return 
$alert;
    } 
Reply With Quote