Development

Changeset 6942

You must first sign up to be able to contribute.

Changeset 6942

Show
Ignore:
Timestamp:
01/05/08 13:34:22 (1 year ago)
Author:
fabien
Message:

replaced _pre_validator and _post_validator by setPreValidator() and setPostValidator() methods (update your forms accordingly)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/validator/sfValidator.class.php

    r6196 r6942  
    296296   * @throws sfValidatorError 
    297297   */ 
    298   abstract protected function doClean($value); 
     298  abstract protected function doClean($value); 
    299299 
    300300  /** 
  • branches/1.1/lib/validator/sfValidatorSchema.class.php

    r6196 r6942  
    2222{ 
    2323  protected 
    24     $fields = array(); 
     24    $fields        = array(), 
     25    $preValidator  = null, 
     26    $postValidator = null; 
    2527 
    2628  /** 
     
    103105    } 
    104106 
    105     $clean = array(); 
     107    $clean = array(); 
    106108    $unused = array_keys($this->fields); 
    107109    $errors = array(); 
    108110 
    109111    // pre validator 
    110     if (isset($this->fields['_pre_validator'])) 
    111     { 
    112       try 
    113       { 
    114         $this->fields['_pre_validator']->clean($values); 
    115       } 
    116       catch (sfValidatorError $e) 
    117       { 
    118         $errors[] = $e; 
    119       } 
     112    try 
     113    { 
     114      $this->preClean($values); 
     115    } 
     116    catch (sfValidatorError $e) 
     117    { 
     118      $errors[] = $e; 
    120119    } 
    121120 
     
    156155    foreach ($unused as $name) 
    157156    { 
    158       if (in_array($name, array('_pre_validator', '_post_validator'))) 
    159       { 
    160         continue; 
    161       } 
    162  
    163157      // validate value 
    164158      try 
     
    173167 
    174168    // post validator 
    175     if (isset($this->fields['_post_validator'])) 
    176     { 
    177       try 
    178       { 
    179         $clean = $this->fields['_post_validator']->clean($clean); 
    180       } 
    181       catch (sfValidatorError $e) 
    182       { 
    183         $errors[] = $e; 
    184       } 
     169    try 
     170    { 
     171      $clean = $this->postClean($clean); 
     172    } 
     173    catch (sfValidatorError $e) 
     174    { 
     175      $errors[] = $e; 
    185176    } 
    186177 
     
    191182 
    192183    return $clean; 
     184  } 
     185 
     186  /** 
     187   * Cleans the input values. 
     188   * 
     189   * This method is the first validator executed by doClean(). 
     190   * 
     191   * It executes the validator returned by getPreValidator() 
     192   * on the global array of values. 
     193   * 
     194   * @param  array The input values 
     195   * 
     196   * @throws sfValidatorError 
     197   */ 
     198  public function preClean($values) 
     199  { 
     200    if (is_null($validator = $this->getPreValidator())) 
     201    { 
     202      return; 
     203    } 
     204 
     205    $validator->clean($values); 
     206  } 
     207 
     208  /** 
     209   * Cleans the input values. 
     210   * 
     211   * This method is the last validator executed by doClean(). 
     212   * 
     213   * It executes the validator returned by getPostValidator() 
     214   * on the global array of cleaned values. 
     215   * 
     216   * @param  array The input values 
     217   * 
     218   * @throws sfValidatorError 
     219   */ 
     220  public function postClean($values) 
     221  { 
     222    if (is_null($validator = $this->getPostValidator())) 
     223    { 
     224      return $values; 
     225    } 
     226 
     227    return $validator->clean($values); 
     228  } 
     229 
     230  /** 
     231   * Sets the pre validator. 
     232   * 
     233   * @param sfValidator A sfValidator instance 
     234   */ 
     235  public function setPreValidator(sfValidator $validator) 
     236  { 
     237    $this->preValidator = $validator; 
     238  } 
     239 
     240  /** 
     241   * Returns the pre validator. 
     242   * 
     243   * @return sfValidator A sfValidator instance 
     244   */ 
     245  public function getPreValidator() 
     246  { 
     247    return $this->preValidator; 
     248  } 
     249 
     250  /** 
     251   * Sets the post validator. 
     252   * 
     253   * @param sfValidator A sfValidator instance 
     254   */ 
     255  public function setPostValidator(sfValidator $validator) 
     256  { 
     257    $this->postValidator = $validator; 
     258  } 
     259 
     260  /** 
     261   * Returns the post validator. 
     262   * 
     263   * @return sfValidator A sfValidator instance 
     264   */ 
     265  public function getPostValidator() 
     266  { 
     267    return $this->postValidator; 
    193268  } 
    194269 
  • branches/1.1/test/unit/validator/sfValidatorSchemaTest.php

    r6197 r6942  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(44, new lime_output_color()); 
     13$t = new lime_test(46, new lime_output_color()); 
    1414 
    1515class PreValidator extends sfValidator 
     
    155155} 
    156156 
    157 $t->diag('pre validators'); 
     157// ->getPreValidator() ->setPreValidator() 
     158$t->diag('->getPreValidator() ->setPreValidator()'); 
    158159$v1 = new sfValidatorString(array('max_length' => 3, 'required' => false)); 
    159160$v2 = new sfValidatorString(array('min_length' => 3, 'required' => false)); 
    160 $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2, '_pre_validator' => new PreValidator())); 
     161$v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2)); 
     162$v->setPreValidator($preValidator = new PreValidator()); 
     163$t->is($v->getPreValidator(), $preValidator, '->getPreValidator() returns the current pre validator'); 
    161164try 
    162165{ 
    163166  $v->clean(array('s1' => 'foo', 's2' => 'bar')); 
    164   $t->fail('->clean() throws an sfValidatorErrorSchema exception if a _pre_validator fails'); 
     167  $t->fail('->clean() throws an sfValidatorErrorSchema exception if a pre-validator fails'); 
    165168  $t->skip('', 2); 
    166169} 
    167170catch (sfValidatorErrorSchema $e) 
    168171{ 
    169   $t->pass('->clean() throws an sfValidatorErrorSchema exception if a _pre_validator fails'); 
     172  $t->pass('->clean() throws an sfValidatorErrorSchema exception if a pre-validator fails'); 
    170173  $t->is(count($e), 1, '->clean() throws an exception with all error messages'); 
    171174  $t->is($e[0]->getCode(), 's1_or_s2', '->clean() throws an exception with all error messages'); 
    172175} 
    173176 
    174 $t->diag('post validators'); 
     177// ->getPostValidator() ->setPostValidator() 
     178$t->diag('->getPostValidator() ->setPostValidator()'); 
    175179$v1 = new sfValidatorString(array('max_length' => 3, 'required' => false)); 
    176180$v2 = new sfValidatorString(array('min_length' => 3, 'required' => false)); 
    177 $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2, '_post_validator' => new PostValidator())); 
     181$v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2)); 
     182$v->setPostValidator($postValidator = new PostValidator()); 
     183$t->is($v->getPostValidator(), $postValidator, '->getPostValidator() returns the current post validator'); 
    178184$t->is($v->clean(array('s1' => 'foo', 's2' => 'bar')), array('s1' => '*foo*', 's2' => '*bar*'), '->clean() executes post validators'); 
    179185 
    180 $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2, '_post_validator' => new Post1Validator())); 
     186$v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2)); 
     187$v->setPostValidator(new Post1Validator()); 
    181188try 
    182189{ 
    183190  $v->clean(array('s1' => 'foo', 's2' => 'foo')); 
    184   $t->fail('->clean() throws an sfValidatorErrorSchema exception if a _post_validator fails'); 
     191  $t->fail('->clean() throws an sfValidatorErrorSchema exception if a post-validator fails'); 
    185192  $t->skip('', 2); 
    186193} 
    187194catch (sfValidatorErrorSchema $e) 
    188195{ 
    189   $t->pass('->clean() throws an sfValidatorErrorSchema exception if a _post_validator fails'); 
     196  $t->pass('->clean() throws an sfValidatorErrorSchema exception if a post-validator fails'); 
    190197  $t->is(count($e), 1, '->clean() throws an exception with all error messages'); 
    191198  $t->is($e[0]->getCode(), 's1_not_equal_s2', '->clean() throws an exception with all error messages'); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.