Changeset 6942
- Timestamp:
- 01/05/08 13:34:22 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/validator/sfValidator.class.php
r6196 r6942 296 296 * @throws sfValidatorError 297 297 */ 298 abstract protected function doClean($value);298 abstract protected function doClean($value); 299 299 300 300 /** branches/1.1/lib/validator/sfValidatorSchema.class.php
r6196 r6942 22 22 { 23 23 protected 24 $fields = array(); 24 $fields = array(), 25 $preValidator = null, 26 $postValidator = null; 25 27 26 28 /** … … 103 105 } 104 106 105 $clean = array();107 $clean = array(); 106 108 $unused = array_keys($this->fields); 107 109 $errors = array(); 108 110 109 111 // 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; 120 119 } 121 120 … … 156 155 foreach ($unused as $name) 157 156 { 158 if (in_array($name, array('_pre_validator', '_post_validator')))159 {160 continue;161 }162 163 157 // validate value 164 158 try … … 173 167 174 168 // 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; 185 176 } 186 177 … … 191 182 192 183 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; 193 268 } 194 269 branches/1.1/test/unit/validator/sfValidatorSchemaTest.php
r6197 r6942 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(4 4, new lime_output_color());13 $t = new lime_test(46, new lime_output_color()); 14 14 15 15 class PreValidator extends sfValidator … … 155 155 } 156 156 157 $t->diag('pre validators'); 157 // ->getPreValidator() ->setPreValidator() 158 $t->diag('->getPreValidator() ->setPreValidator()'); 158 159 $v1 = new sfValidatorString(array('max_length' => 3, 'required' => false)); 159 160 $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'); 161 164 try 162 165 { 163 166 $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'); 165 168 $t->skip('', 2); 166 169 } 167 170 catch (sfValidatorErrorSchema $e) 168 171 { 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'); 170 173 $t->is(count($e), 1, '->clean() throws an exception with all error messages'); 171 174 $t->is($e[0]->getCode(), 's1_or_s2', '->clean() throws an exception with all error messages'); 172 175 } 173 176 174 $t->diag('post validators'); 177 // ->getPostValidator() ->setPostValidator() 178 $t->diag('->getPostValidator() ->setPostValidator()'); 175 179 $v1 = new sfValidatorString(array('max_length' => 3, 'required' => false)); 176 180 $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'); 178 184 $t->is($v->clean(array('s1' => 'foo', 's2' => 'bar')), array('s1' => '*foo*', 's2' => '*bar*'), '->clean() executes post validators'); 179 185 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()); 181 188 try 182 189 { 183 190 $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'); 185 192 $t->skip('', 2); 186 193 } 187 194 catch (sfValidatorErrorSchema $e) 188 195 { 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'); 190 197 $t->is(count($e), 1, '->clean() throws an exception with all error messages'); 191 198 $t->is($e[0]->getCode(), 's1_not_equal_s2', '->clean() throws an exception with all error messages');

