Changeset 1642

Show
Ignore:
Timestamp:
02/04/07 22:18:34 (2 years ago)
Author:
david
Message:

each container has its own validation manager now. all error-related methods moved from request to validation manager. sample app adjusted accordingly. closes #406, fixes #405

Location:
branches/0.11
Files:
9 modified

Legend:

Unmodified
Added
Removed
  • branches/0.11/samples/app/modules/Default/templates/Master.php

    r1635 r1642  
    160160    <div id="content"> 
    161161      <h2><?php echo $template['title']; ?></h2> 
    162 <?php if($rq->hasErrors()): foreach($rq->getErrorMessages() as $error): ?> 
     162<?php if($container->getValidationManager()->hasErrors()): foreach($container->getValidationManager()->getErrorMessages() as $error): ?> 
    163163      <p class="error"><?php echo $error['message']; ?></p> 
    164164<?php endforeach; endif; ?> 
  • branches/0.11/src/config/AgaviFactoryConfigHandler.class.php

    r1601 r1642  
    162162        $data['validation_manager']['params'] = $this->getItemParameters($cfg->validation_manager, $data['validation_manager']['params']); 
    163163 
    164         $data['validation_manager_code'] =  '$this->validationManager = new ' . $data['validation_manager']['class'] . '();' . "\n" . 
    165                                           '$this->validationManager->initialize($this, ' . var_export($data['validation_manager']['params'], true) . ');'; 
     164        $data['validation_manager_code'] =  '$this->factories["validation_manager"] = array("class" => "' . $data['validation_manager']['class'] . '", "parameters" => ' . var_export($data['validation_manager']['params'], true) . ');'; 
    166165      } 
    167166 
  • branches/0.11/src/controller/AgaviExecutionContainer.class.php

    r1639 r1642  
    3333   */ 
    3434  protected $context = null; 
     35   
     36  /** 
     37   * @var        AgaviValidationManager The validation manager instance. 
     38   */ 
     39  protected $validationManager = null; 
    3540   
    3641  /** 
     
    361366   
    362367  /** 
     368   * Retrieve the ValidationManager 
     369   * 
     370   * @return     AgaviValidationManager The container's ValidationManager  
     371   *                                    implementation instance. 
     372   * 
     373   * @author     David Zuelke <dz@bitxtender.com> 
     374   * @since      0.11.0 
     375   */ 
     376  public function getValidationManager() 
     377  { 
     378    if($this->validationManager === null) { 
     379      $vmfi = $this->context->getFactoryInfo('validation_manager'); 
     380      $this->validationManager = new $vmfi['class'](); 
     381      $this->validationManager->initialize($this->context, $vmfi['parameters']); 
     382    } 
     383    return $this->validationManager; 
     384  } 
     385   
     386  /** 
    363387   * Retrieve this container's request data holder instance. 
    364388   * 
  • branches/0.11/src/core/AgaviContext.class.php

    r1568 r1642  
    4848    'execution_filter' => null, 
    4949    'filter_chain' => null, 
    50     'security_filter' => null 
     50    'response' => null, 
     51    'security_filter' => null, 
     52    'validation_manager' => null, 
    5153  ); 
    5254   
     
    8688   */ 
    8789  protected $user = null; 
    88    
    89   /** 
    90    * @var        AgaviValidationManager A ValidationManager instance. 
    91    */ 
    92   protected $validationManager = null; 
    9390   
    9491  /** 
     
    496493    return $this->user; 
    497494  } 
    498    
    499   /** 
    500    * Retrieve the ValidationManager 
    501    * 
    502    * @return     AgaviValidationManager The current ValidationManager  
    503    *                                    implementation instance. 
    504    * 
    505    * @author     David Zuelke <dz@bitxtender.com> 
    506    * @since      0.11.0 
    507    */ 
    508   public function getValidationManager() 
    509   { 
    510     return $this->validationManager; 
    511   } 
    512495} 
    513496 
  • branches/0.11/src/filter/AgaviExecutionFilter.class.php

    r1638 r1642  
    460460    $controller = $this->context->getController(); 
    461461    $request = $this->context->getRequest(); 
    462     $validationManager = $this->context->getValidationManager(); 
    463     // clear the validator manager for reuse 
    464     $validationManager->clear(); 
     462    $validationManager = $container->getValidationManager(); 
    465463 
    466464    // get the current action instance 
  • branches/0.11/src/filter/AgaviFormPopulationFilter.class.php

    r1626 r1642  
    8787    $req = $this->getContext()->getRequest(); 
    8888     
     89    $vm = null; 
     90     
    8991    $cfg = array_merge(array('populate' => null, 'skip' => null), $this->getParameters(), $req->getAttributes('org.agavi.filter.FormPopulationFilter')); 
    9092     
     
    230232          continue; 
    231233        } 
     234      } 
     235       
     236      // no validation manager set yet? let's do that. the later, the better. 
     237      if($vm === null) { 
     238        $vm = $container->getValidationManager(); 
    232239      } 
    233240       
     
    303310         
    304311        // there's an error with the element's name in the request? good. let's give the baby a class! 
    305         if($req->hasError($pname)) { 
     312        if($vm->hasError($pname)) { 
    306313          $element->setAttribute('class', preg_replace('/\s*$/', ' ' . $cfg['error_class'], $element->getAttribute('class'))); 
    307314          // assign the class to all implicit labels 
  • branches/0.11/src/request/AgaviRequest.class.php

    r1568 r1642  
    3131abstract class AgaviRequest extends AgaviAttributeHolder 
    3232{ 
    33  
    3433  /** 
    3534   * @var        array An associative array of attributes 
     
    8685  } 
    8786 
    88  
    89   /** 
    90    * Retrieve an error message. 
    91    * 
    92    * @param      string An error name. 
    93    * 
    94    * @return     string The error message. 
     87  /** 
     88   * Retrieve this request's method. 
     89   * 
     90   * @return     string The request method name 
    9591   * 
    9692   * @author     Sean Kerr <skerr@mojavi.org> 
    97    * @author     Dominik del Bondio <ddb@bitxtender.com> 
    98    * @since      0.9.0 
    99    */ 
    100   public function getError($name) 
    101   { 
    102     $vm = $this->getContext()->getValidationManager(); 
    103     $incidents = $vm->getFieldIncidents($name, AgaviValidator::NOTICE); 
    104  
    105     if(count($incidents) == 0) { 
    106       return null; 
    107     } 
    108  
    109     $errors = $incidents[0]->getErrors(); 
    110     return $errors[0]->getMessage(); 
    111   } 
    112  
    113   /** 
    114    * Retrieve an array of error names. 
    115    * 
    116    * @return     array An indexed array of error names. 
    117    * 
    118    * @author     Sean Kerr <skerr@mojavi.org> 
    119    * @author     Dominik del Bondio <ddb@bitxtender.com> 
    120    * @since      0.9.0 
    121    */ 
    122   public function getErrorNames() 
    123   { 
    124     return $this->getContext()->getValidationManager()->getFailedFields(); 
    125   } 
    126  
    127   /** 
    128    * Retrieve an array of errors. 
    129    * 
    130    * @param      string An optional error name. 
    131    * 
    132    * @return     array An associative array of errors(if no name was given) as 
    133    *                   an array with the error messages (key 'messages') and 
    134    *                   the validators (key 'validators') which failed. 
    135    * 
    136    * @author     Sean Kerr <skerr@mojavi.org> 
    137    * @author     Dominik del Bondio <ddb@bitxtender.com> 
    138    * @since      0.9.0 
    139    */ 
    140   public function getErrors($name = null) 
    141   { 
    142     $vm = $this->getContext()->getValidationManager(); 
    143     $errors = array(); 
    144  
    145     foreach($vm->getIncidents(AgaviValidator::NOTICE) as $incident) { 
    146       $validator = $incident->getValidator(); 
    147       foreach($incident->getErrors() as $error) { 
    148         $msg = $error->getMessage(); 
    149         foreach($error->getFields() as $field) { 
    150           if(!isset($errors[$field])) { 
    151             $errors[$field] = array('messages' => array(), 'validators' => array()); 
    152           } 
    153           $errors[$field]['messages'][] = $msg; 
    154           if($validator) { 
    155             $errors[$field]['validators'][] = $validator->getName(); 
    156           } 
    157         } 
    158       } 
    159     } 
    160  
    161     if($name === null) { 
    162       return $errors; 
    163     } else { 
    164       return isset($errors[$name]) ? $errors[$name] : null; 
    165     } 
    166   } 
    167  
    168   /** 
    169    * Retrieve an array of error Messages. 
    170    * 
    171    * @param      string An optional error name. 
    172    * 
    173    * @return     array An indexed array of error messages (if a name was given) 
    174    *                   or an indexed array in this format: 
    175    *                   array('message' => string, 'errors' => array(string)) 
    176    * 
    177    * @author     Dominik del Bondio <ddb@bitxtender.com> 
    178    * @since      0.11.0 
    179    */ 
    180   public function getErrorMessages($name = null) 
    181   { 
    182     $vm = $this->getContext()->getValidationManager(); 
    183  
    184     if($name !== null) { 
    185       $incidents = $vm->getFieldIncidents($name, AgaviValidator::NOTICE); 
    186       $msgs = array(); 
    187       foreach($incidents as $incident) { 
    188         foreach($incident->getErrors() as $error) { 
    189           $msgs[] = $error->getMessage(); 
    190         } 
    191       } 
    192       return $msgs; 
    193     } else { 
    194       $msgs = array(); 
    195  
    196       $incidents = $vm->getIncidents(AgaviValidator::NOTICE); 
    197       $msgs = array(); 
    198       foreach($incidents as $incident) { 
    199         foreach($incident->getErrors() as $error) { 
    200           $msgs[] = array('message' => $error->getMessage(), 'errors' => $error->getFields()); 
    201         } 
    202       } 
    203       return $msgs; 
    204     } 
    205   } 
    206  
    207   /** 
    208    * Retrieve this request's method. 
    209    * 
    210    * @return     string The request method name 
    211    * 
    212    * @author     Sean Kerr <skerr@mojavi.org> 
    21393   * @author     David Zuelke <dz@bitxtender.com> 
    21494   * @since      0.9.0 
     
    21797  { 
    21898    return $this->method; 
    219   } 
    220  
    221   /** 
    222    * Indicates whether or not an error exists. 
    223    * 
    224    * @param      string An error name. 
    225    * 
    226    * @return     bool true, if the error exists, otherwise false. 
    227    * 
    228    * @author     Sean Kerr <skerr@mojavi.org> 
    229    * @author     Dominik del Bondio <ddb@bitxtender.com> 
    230    * @since      0.9.0 
    231    */ 
    232   public function hasError($name) 
    233   { 
    234     return $this->getContext()->getValidationManager()->isFieldFailed($name); 
    235   } 
    236  
    237  
    238   /** 
    239    * Indicates whether or not any errors exist. 
    240    * 
    241    * @return     bool true, if any error exist, otherwise false. 
    242    * 
    243    * @author     Sean Kerr <skerr@mojavi.org> 
    244    * @author     Dominik del Bondio <ddb@bitxtender.com> 
    245    * @since      0.9.0 
    246    */ 
    247   public function hasErrors() 
    248   { 
    249     return $this->getContext()->getValidationManager()->getResult() > AgaviValidator::NOTICE; 
    25099  } 
    251100 
     
    278127      $this->actionAccessor = $parameters['action_accessor']; 
    279128    } 
    280   } 
    281  
    282   /** 
    283    * Set an error. 
    284    * 
    285    * @param      string An error name. 
    286    * @param      string An error message. 
    287    * 
    288    * @author     Dominik del Bondio <ddb@bitxtender.com> 
    289    * @since      0.9.0 
    290    */ 
    291   public function setError($name, $message) 
    292   { 
    293     $vm = $this->getContext()->getValidationManager(); 
    294     $incident = new AgaviValidationIncident(null, AgaviValidator::ERROR); 
    295     $incident->addError(new AgaviValidationError($message, null, array($name))); 
    296     $vm->addIncident($incident); 
    297   } 
    298  
    299  
    300   /** 
    301    * Set an array of errors 
    302    * 
    303    * If an existing error name matches any of the keys in the supplied 
    304    * array, the associated message will be appended to the messages array. 
    305    * 
    306    * @param      array An associative array of errors and their associated 
    307    *                   messages. 
    308    * 
    309    * @author     Dominik del Bondio <ddb@bitxtender.com> 
    310    * @since      0.9.0 
    311    */ 
    312   public function setErrors(array $errors) 
    313   { 
    314     $vm = $this->getContext()->getValidationManager(); 
    315     $incident = new AgaviValidationIncident(null, AgaviValidator::ERROR); 
    316     foreach($errors as $name => $error) { 
    317       $incident->addError(new AgaviValidationError($error, null, array($name))); 
    318     } 
    319  
    320     $vm->addIncident($incident); 
    321129  } 
    322130 
  • branches/0.11/src/validator/AgaviValidationManager.class.php

    r1624 r1642  
    666666    return array_values(array_unique($fields)); 
    667667  } 
     668 
     669  /** 
     670   * Retrieve an error message. 
     671   * 
     672   * @param      string An error name. 
     673   * 
     674   * @return     string The error message. 
     675   * 
     676   * @author     Sean Kerr <skerr@mojavi.org> 
     677   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     678   * @since      0.9.0 
     679   */ 
     680  public function getError($name) 
     681  { 
     682    $incidents = $this->getFieldIncidents($name, AgaviValidator::NOTICE); 
     683 
     684    if(count($incidents) == 0) { 
     685      return null; 
     686    } 
     687 
     688    $errors = $incidents[0]->getErrors(); 
     689    return $errors[0]->getMessage(); 
     690  } 
     691 
     692  /** 
     693   * Retrieve an array of error names. 
     694   * 
     695   * @return     array An indexed array of error names. 
     696   * 
     697   * @author     Sean Kerr <skerr@mojavi.org> 
     698   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     699   * @since      0.9.0 
     700   */ 
     701  public function getErrorNames() 
     702  { 
     703    return $this->getFailedFields(); 
     704  } 
     705 
     706  /** 
     707   * Retrieve an array of errors. 
     708   * 
     709   * @param      string An optional error name. 
     710   * 
     711   * @return     array An associative array of errors(if no name was given) as 
     712   *                   an array with the error messages (key 'messages') and 
     713   *                   the validators (key 'validators') which failed. 
     714   * 
     715   * @author     Sean Kerr <skerr@mojavi.org> 
     716   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     717   * @since      0.9.0 
     718   */ 
     719  public function getErrors($name = null) 
     720  { 
     721    $errors = array(); 
     722 
     723    foreach($this->getIncidents(AgaviValidator::NOTICE) as $incident) { 
     724      $validator = $incident->getValidator(); 
     725      foreach($incident->getErrors() as $error) { 
     726        $msg = $error->getMessage(); 
     727        foreach($error->getFields() as $field) { 
     728          if(!isset($errors[$field])) { 
     729            $errors[$field] = array('messages' => array(), 'validators' => array()); 
     730          } 
     731          $errors[$field]['messages'][] = $msg; 
     732          if($validator) { 
     733            $errors[$field]['validators'][] = $validator->getName(); 
     734          } 
     735        } 
     736      } 
     737    } 
     738 
     739    if($name === null) { 
     740      return $errors; 
     741    } else { 
     742      return isset($errors[$name]) ? $errors[$name] : null; 
     743    } 
     744  } 
     745 
     746  /** 
     747   * Retrieve an array of error Messages. 
     748   * 
     749   * @param      string An optional error name. 
     750   * 
     751   * @return     array An indexed array of error messages (if a name was given) 
     752   *                   or an indexed array in this format: 
     753   *                   array('message' => string, 'errors' => array(string)) 
     754   * 
     755   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     756   * @since      0.11.0 
     757   */ 
     758  public function getErrorMessages($name = null) 
     759  { 
     760 
     761    if($name !== null) { 
     762      $incidents = $this->getFieldIncidents($name, AgaviValidator::NOTICE); 
     763      $msgs = array(); 
     764      foreach($incidents as $incident) { 
     765        foreach($incident->getErrors() as $error) { 
     766          $msgs[] = $error->getMessage(); 
     767        } 
     768      } 
     769      return $msgs; 
     770    } else { 
     771      $msgs = array(); 
     772 
     773      $incidents = $this->getIncidents(AgaviValidator::NOTICE); 
     774      $msgs = array(); 
     775      foreach($incidents as $incident) { 
     776        foreach($incident->getErrors() as $error) { 
     777          $msgs[] = array('message' => $error->getMessage(), 'errors' => $error->getFields()); 
     778        } 
     779      } 
     780      return $msgs; 
     781    } 
     782  } 
     783 
     784  /** 
     785   * Indicates whether or not an error exists. 
     786   * 
     787   * @param      string An error name. 
     788   * 
     789   * @return     bool true, if the error exists, otherwise false. 
     790   * 
     791   * @author     Sean Kerr <skerr@mojavi.org> 
     792   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     793   * @since      0.9.0 
     794   */ 
     795  public function hasError($name) 
     796  { 
     797    return $this->isFieldFailed($name); 
     798  } 
     799 
     800  /** 
     801   * Indicates whether or not any errors exist. 
     802   * 
     803   * @return     bool true, if any error exist, otherwise false. 
     804   * 
     805   * @author     Sean Kerr <skerr@mojavi.org> 
     806   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     807   * @since      0.9.0 
     808   */ 
     809  public function hasErrors() 
     810  { 
     811    return $this->getResult() > AgaviValidator::NOTICE; 
     812  } 
     813 
     814  /** 
     815   * Set an error. 
     816   * 
     817   * @param      string An error name. 
     818   * @param      string An error message. 
     819   * 
     820   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     821   * @since      0.9.0 
     822   */ 
     823  public function setError($name, $message) 
     824  { 
     825    $incident = new AgaviValidationIncident(null, AgaviValidator::ERROR); 
     826    $incident->addError(new AgaviValidationError($message, null, array($name))); 
     827    $this->addIncident($incident); 
     828  } 
     829 
     830 
     831  /** 
     832   * Set an array of errors 
     833   * 
     834   * If an existing error name matches any of the keys in the supplied 
     835   * array, the associated message will be appended to the messages array. 
     836   * 
     837   * @param      array An associative array of errors and their associated 
     838   *                   messages. 
     839   * 
     840   * @author     Dominik del Bondio <ddb@bitxtender.com> 
     841   * @since      0.9.0 
     842   */ 
     843  public function setErrors(array $errors) 
     844  { 
     845    $incident = new AgaviValidationIncident(null, AgaviValidator::ERROR); 
     846    foreach($errors as $name => $error) { 
     847      $incident->addError(new AgaviValidationError($error, null, array($name))); 
     848    } 
     849 
     850    $this->addIncident($incident); 
     851  } 
    668852} 
    669853?> 
  • branches/0.11/src/validator/AgaviValidator.class.php

    r1631 r1642  
    290290    $this->curBase = $parent->getBase(); 
    291291    $this->parentContainer = $parent; 
     292    $this->validationManager = $this->parentContainer; 
     293    while(!($this->validationManager instanceof AgaviValidationManager) && ($this->validationManager = $this->validationManager->getParentContainer())) { 
     294    } 
    292295  } 
    293296 
     
    519522    $cp = $this->curBase->pushRetNew($name); 
    520523    $cp->setValueFromArray($array, $value); 
    521     $this->getContext()->getValidationManager()->addFieldResult($this, $cp->__toString(), AgaviValidator::NOT_PROCESSED); 
     524    if($this->validationManager !== null) { 
     525      $this->validationManager->addFieldResult($this, $cp->__toString(), AgaviValidator::NOT_PROCESSED); 
     526    } 
    522527  } 
    523528 
     
    565570      } 
    566571 
    567       $vm = $this->getContext()->getValidationManager(); 
    568       foreach($fieldnames as $fieldname) { 
    569         $vm->addFieldResult($this, $fieldname, $result); 
    570       } 
    571  
    572       if($this->incident) { 
    573         $vm->addIncident($this->incident); 
    574         $this->incident = null; 
    575       } 
    576  
     572      if($this->validationManager !== null) { 
     573        foreach($fieldnames as $fieldname) { 
     574          $this->validationManager->addFieldResult($this, $fieldname, $result); 
     575        } 
     576 
     577        if($this->incident) { 
     578          $this->validationManager->addIncident($this->incident); 
     579        } 
     580      } 
     581 
     582      $this->incident = null; 
    577583      // put dependencies provided by this validator into manager 
    578584      if($result == self::SUCCESS && count($this->getParameter('provides')) > 0) {