Changeset 5816
- Timestamp:
- 11/02/07 16:59:47 (10 months ago)
- Files:
-
- trunk/lib/validator/sfValidator.class.php (modified) (6 diffs)
- trunk/lib/validator/sfValidatorAll.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorAny.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorBoolean.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorCallback.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorChoice.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorDate.class.php (modified) (4 diffs)
- trunk/lib/validator/sfValidatorDecorator.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorEmail.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorInteger.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorNumber.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorRegex.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorSchema.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorSchemaCompare.class.php (modified) (4 diffs)
- trunk/lib/validator/sfValidatorSchemaFilter.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorString.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorUrl.class.php (modified) (2 diffs)
- trunk/test/unit/validator/sfValidatorBooleanTest.php (modified) (1 diff)
- trunk/test/unit/validator/sfValidatorDateTest.php (modified) (3 diffs)
- trunk/test/unit/validator/sfValidatorDecoratorTest.php (modified) (3 diffs)
- trunk/test/unit/validator/sfValidatorFromDescriptionTest.php (modified) (1 diff)
- trunk/test/unit/validator/sfValidatorIntegerTest.php (modified) (1 diff)
- trunk/test/unit/validator/sfValidatorNumberTest.php (modified) (1 diff)
- trunk/test/unit/validator/sfValidatorRegexTest.php (modified) (2 diffs)
- trunk/test/unit/validator/sfValidatorSchemaTest.php (modified) (2 diffs)
- trunk/test/unit/validator/sfValidatorStringTest.php (modified) (1 diff)
- trunk/test/unit/validator/sfValidatorTest.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/validator/sfValidator.class.php
r5753 r5816 25 25 26 26 protected 27 $messages = array(), 28 $options = array(); 27 $requiredOptions = array(), 28 $defaultMessages = array(), 29 $defaultOptions = array(), 30 $messages = array(), 31 $options = array(); 29 32 30 33 /** … … 42 45 public function __construct($options = array(), $messages = array()) 43 46 { 44 $this-> messages = array('required' => 'Required.', 'invalid' => 'Invalid.');45 $this-> options = array('required' => true, 'trim' => false, 'empty_value' => null);47 $this->options = array_merge(array('required' => true, 'trim' => false, 'empty_value' => null), $this->options); 48 $this->messages = array_merge(array('required' => 'Required.', 'invalid' => 'Invalid.'), $this->messages); 46 49 47 50 $this->configure($options, $messages); 51 52 $this->setDefaultOptions($this->getOptions()); 53 $this->setDefaultMessages($this->getMessages()); 54 55 // check option names 56 if ($diff = array_diff(array_keys($options), array_merge(array_keys($this->options), $this->requiredOptions))) 57 { 58 throw new sfException(sprintf('%s does not support the following options: \'%s\'.', get_class($this), implode('\', \'', $diff))); 59 } 60 61 // check error code names 62 if ($diff = array_diff(array_keys($messages), array_keys($this->messages))) 63 { 64 throw new sfException(sprintf('%s does not support the following error codes: \'%s\'.', get_class($this), implode('\', \'', $diff))); 65 } 66 67 // check required options 68 if ($diff = array_diff($this->requiredOptions, array_keys($options))) 69 { 70 throw new sfException(sprintf('%s requires the following options: \'%s\'.', get_class($this), implode('\', \'', $diff))); 71 } 48 72 49 73 $this->options = array_merge($this->options, $options); 50 74 $this->messages = array_merge($this->messages, $messages); 75 } 76 77 /** 78 * Configures the current validator. 79 * 80 * This method allows each validator to add options and error messages 81 * during validator creation. 82 * 83 * If some options and messages are given in the sfValidator constructor 84 * they will take precedence over the options and messages you configure 85 * in this method. 86 * 87 * @param array An array of options 88 * @param array An array of error messages 89 * 90 * @see __construct() 91 */ 92 protected function configure($options = array(), $messages = array()) 93 { 51 94 } 52 95 … … 199 242 200 243 /** 244 * Sets the charset to use when validating strings. 245 * 246 * @param string The charset 247 */ 248 static public function setCharset($charset) 249 { 250 self::$charset = $charset; 251 } 252 253 /** 254 * Returns the charset to use when validating strings. 255 * 256 * @return string The charset (default to UTF-8) 257 */ 258 static public function getCharset() 259 { 260 return self::$charset; 261 } 262 263 /** 264 * Returns true if the value is empty. 265 * 266 * @param mixed The input value 267 * 268 * @return Boolean true if the value is empty, false otherwise 269 */ 270 protected function isEmpty($value) 271 { 272 return in_array($value, array(null, '')); 273 } 274 275 /** 276 * Returns an empty value for this validator. 277 * 278 * @return mixed The empty value for this validator 279 */ 280 protected function getEmptyValue() 281 { 282 return $this->getOption('empty_value'); 283 } 284 285 /** 201 286 * Returns an array of all error codes for this validator. 202 287 * 203 * Subclasses of sfValidator may override this method to register204 * their own error codes.205 *206 * By default this method return required and invalid as errors codes.207 *208 288 * @return array An array of possible error codes 209 */ 210 public function getErrorCodes() 211 { 212 return array('required', 'invalid'); 213 } 214 215 /** 216 * Sets the charset to use when validating strings. 217 * 218 * @param string The charset 219 */ 220 static public function setCharset($charset) 221 { 222 self::$charset = $charset; 223 } 224 225 /** 226 * Returns the charset to use when validating strings. 227 * 228 * @return string The charset (default to UTF-8) 229 */ 230 static public function getCharset() 231 { 232 return self::$charset; 233 } 234 235 /** 236 * Returns true if the value is empty. 237 * 238 * @param mixed The input value 239 * 240 * @return Boolean true if the value is empty, false otherwise 241 */ 242 protected function isEmpty($value) 243 { 244 return in_array($value, array(null, '')); 245 } 246 247 /** 248 * Returns an empty value for this validator. 249 * 250 * @return mixed The empty value for this validator 251 */ 252 protected function getEmptyValue() 253 { 254 return $this->getOption('empty_value'); 255 } 256 257 /** 258 * Configures the current validator. 259 * 260 * This method allows each validator to add options and error messages 261 * during validator creation. 262 * 263 * If some options and messages are given in the sfValidator constructor 264 * they will take precedence over the options and messages you configure 265 * in this method. 266 * 267 * @param array An array of options 268 * @param array An array of error messages 269 * 270 * @see __construct() 271 */ 272 protected function configure($options = array(), $messages = array()) 273 { 289 * 290 * @see getDefaultMessages() 291 */ 292 final public function getErrorCodes() 293 { 294 return array_keys($this->getDefaultMessages()); 295 } 296 297 /** 298 * Returns default messages for all possible error codes. 299 * 300 * @return array An array of default error codes and messages 301 */ 302 public function getDefaultMessages() 303 { 304 return $this->defaultMessages; 305 } 306 307 /** 308 * Sets default messages for all possible error codes. 309 * 310 * @param array An array of default error codes and messages 311 */ 312 protected function setDefaultMessages($messages) 313 { 314 $this->defaultMessages = $messages; 315 } 316 317 /** 318 * Returns default option values. 319 * 320 * @return array An array of default option values 321 */ 322 public function getDefaultOptions() 323 { 324 return $this->defaultOptions; 325 } 326 327 /** 328 * Sets default option values. 329 * 330 * @param array An array of default option values 331 */ 332 protected function setDefaultOptions($options) 333 { 334 $this->defaultOptions = $options; 335 } 336 337 /** 338 * Adds a required option. 339 * 340 * @param string The option name 341 */ 342 public function addRequiredOption($name) 343 { 344 $this->requiredOptions[] = $name; 345 } 346 347 /** 348 * Returns all required option names. 349 * 350 * @param array An array of required option names 351 */ 352 public function getRequiredOptions() 353 { 354 return $this->requiredOptions; 274 355 } 275 356 … … 295 376 296 377 /** 378 * Returns all error messages with non default values. 379 * 380 * @return string A string representation of the error messages 381 */ 382 protected function getMessagesWithoutDefaults() 383 { 384 $messages = $this->messages; 385 386 // remove default option values 387 foreach ($this->getDefaultMessages() as $key => $value) 388 { 389 if (array_key_exists($key, $messages) && $messages[$key] === $value) 390 { 391 unset($messages[$key]); 392 } 393 } 394 395 return $messages; 396 } 397 398 /** 297 399 * Returns all options with non default values. 298 400 * … … 304 406 305 407 // remove default option values 306 $reflection = new ReflectionClass(get_class($this)); 307 $default = $reflection->newInstanceArgs(func_get_args()); 308 foreach ($default->getOptions() as $key => $value) 408 foreach ($this->getDefaultOptions() as $key => $value) 309 409 { 310 410 if (array_key_exists($key, $options) && $options[$key] === $value) … … 316 416 return $options; 317 417 } 318 319 /**320 * Returns all error messages with non default values.321 *322 * @return string A string representation of the error messages323 */324 protected function getMessagesWithoutDefaults()325 {326 $messages = $this->messages;327 328 // remove default option values329 $reflection = new ReflectionClass(get_class($this));330 $default = $reflection->newInstanceArgs(func_get_args());331 foreach ($default->getMessages() as $key => $value)332 {333 if (array_key_exists($key, $messages) && $messages[$key] === $value)334 {335 unset($messages[$key]);336 }337 }338 339 return $messages;340 }341 418 } trunk/lib/validator/sfValidatorAll.class.php
r5753 r5816 55 55 } 56 56 57 if (!isset($messages['invalid'])) 58 { 59 $messages['invalid'] = null; 60 } 57 parent::__construct($options, $messages); 58 } 61 59 62 parent::__construct($options, $messages); 60 /** 61 * @see sfValidator 62 */ 63 public function configure($options = array(), $messages = array()) 64 { 65 $this->setMessage('invalid', null); 63 66 } 64 67 … … 145 148 } 146 149 147 return sprintf("%s(%s%s)", 148 str_repeat(' ', $indent), 149 $validators, 150 str_repeat(' ', $indent) 151 ); 150 return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent)); 152 151 } 153 152 } trunk/lib/validator/sfValidatorAny.class.php
r5753 r5816 55 55 } 56 56 57 if (!isset($messages['invalid'])) 58 { 59 $messages['invalid'] = null; 60 } 57 parent::__construct($options, $messages); 58 } 61 59 62 parent::__construct($options, $messages); 60 /** 61 * @see sfValidator 62 */ 63 public function configure($options = array(), $messages = array()) 64 { 65 $this->setMessage('invalid', null); 63 66 } 64 67 … … 139 142 } 140 143 141 return sprintf("%s(%s%s)", 142 str_repeat(' ', $indent), 143 $validators, 144 str_repeat(' ', $indent) 145 ); 144 return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent)); 146 145 } 147 146 } trunk/lib/validator/sfValidatorBoolean.class.php
r5634 r5816 33 33 $this->setOption('true_values', array('true', 't', 'yes', 'y', 'on', '1')); 34 34 $this->setOption('false_values', array('false', 'f', 'no', 'n', 'off', '0')); 35 36 35 $this->setOption('required', false); 37 36 } trunk/lib/validator/sfValidatorCallback.class.php
r5753 r5816 20 20 { 21 21 /** 22 * Con structor.22 * Configures the current validator. 23 23 * 24 24 * Available options: 25 25 * 26 * * callback: A valid PHP callback 26 * * callback: A valid PHP callback (required) 27 27 * 28 * @see sfValidator29 */30 public function __construct($options = array(), $messages = array())31 {32 if (!isset($options['callback']))33 {34 throw new sfException('The "callback" option is mandatory.');35 }36 37 parent::__construct($options, $messages);38 }39 40 /**41 28 * @see sfValidator 42 29 */ 43 30 protected function configure($options = array(), $messages = array()) 44 31 { 45 $this->options['required'] = false; 32 $this->addRequiredOption('callback'); 33 $this->setOption('required', false); 46 34 } 47 35 … … 53 41 return call_user_func($this->getOption('callback'), $this, $value); 54 42 } 55 56 /**57 * @see sfValidator58 */59 protected function getOptionsWithoutDefaults()60 {61 return parent::getOptionsWithoutDefaults(array('callback' => array('--fake--')));62 }63 64 /**65 * @see sfValidator66 */67 protected function getMessagesWithoutDefaults()68 {69 return parent::getMessagesWithoutDefaults(array('callback' => array('--fake--')));70 }71 43 } trunk/lib/validator/sfValidatorChoice.class.php
r5757 r5816 20 20 { 21 21 /** 22 * Con structor.22 * Configures the current validator. 23 23 * 24 24 * Available options: 25 25 * 26 * * choices: An array of expected values 26 * * choices: An array of expected values (required) 27 27 * 28 28 * @see sfValidator 29 29 */ 30 p ublic function __construct($options = array(), $messages = array())30 protected function configure($options = array(), $messages = array()) 31 31 { 32 if (!isset($options['choices'])) 33 { 34 throw new sfException('The "choices" option is mandatory.'); 35 } 36 37 parent::__construct($options, $messages); 32 $this->addRequiredOption('choices'); 38 33 } 39 34 … … 50 45 return $value; 51 46 } 52 53 /**54 * @see sfValidator55 */56 protected function getOptionsWithoutDefaults()57 {58 return parent::getOptionsWithoutDefaults(array('choices' => array('--fake--')));59 }60 61 /**62 * @see sfValidator63 */64 protected function getMessagesWithoutDefaults()65 {66 return parent::getMessagesWithoutDefaults(array('choices' => array('--fake--')));67 }68 47 } trunk/lib/validator/sfValidatorDate.class.php
r5581 r5816 35 35 $this->setMessage('bad_format', '"%value%" does not match the date format (%date_format%).'); 36 36 37 $this->setOption('date_format', null); 37 38 $this->setOption('with_time', false); 38 39 $this->setOption('date_output', 'Y-m-d'); … … 45 46 protected function doClean($value) 46 47 { 47 if ($regex = $this->getOption('date_format')) 48 if (is_array($value)) 49 { 50 $clean = $this->convertDateArrayToTimestamp($value); 51 } 52 else if ($regex = $this->getOption('date_format')) 48 53 { 49 54 if (!preg_match($regex, $value, $match)) … … 52 57 } 53 58 54 if ($this->getOption('with_time')) 55 { 56 $clean = mktime(isset($match['hour']) ? $match['hour'] : 0, isset($match['minute']) ? $match['minute'] : 0, isset($match['second']) ? $match['second'] : 0, $match['month'], $match['day'], $match['year']); 57 } 58 else 59 { 60 $clean = mktime(0, 0, 0, $match['month'], $match['day'], $match['year']); 61 } 59 $clean = $this->convertDateArrayToTimestamp($match); 62 60 } 63 61 else if (!ctype_digit($value)) 64 62 { 65 63 $clean = strtotime($value); 66 if ( $clean === -1 || $clean === false)64 if (false === $clean) 67 65 { 68 66 throw new sfValidatorError($this, 'invalid', array('value' => $value)); … … 78 76 79 77 /** 80 * @see sfValidator 78 * Converts an array representing a date to a timestamp. 79 * 80 * The array can contains the following keys: year, month, day, hour, minute, second 81 * 82 * @param array An array of date elements 83 * 84 * @return integer A timestamp 81 85 */ 82 p ublic function getErrorCodes()86 protected function convertDateArrayToTimestamp($value) 83 87 { 84 return array_merge(parent::getErrorCodes(), array('date_format')); 88 if ($this->getOption('with_time')) 89 { 90 $clean = mktime(isset($value['hour']) ? $value['hour'] : 0, isset($value['minute']) ? $value['minute'] : 0, isset($value['second']) ? $value['second'] : 0, $value['month'], $value['day'], $value['year']); 91 } 92 else 93 { 94 $clean = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']); 95 } 96 97 if (false === $clean) 98 { 99 throw new sfValidatorError($this, 'invalid', array('value' => var_export($value, true))); 100 } 101 102 return $clean; 85 103 } 86 104 } trunk/lib/validator/sfValidatorDecorator.class.php
r5753 r5816 149 149 * @see sfValidator 150 150 */ 151 public function getErrorCodes()151 public function asString($indent = 0) 152 152 { 153 return $this-> getValidator()->getErrorCodes();153 return $this->validator->asString($indent); 154 154 } 155 155 … … 157 157 * @see sfValidator 158 158 */ 159 public function asString($indent = 0)159 public function getDefaultOptions() 160 160 { 161 return $this->getValidator()->asString($indent); 161 return $this->validator->getDefaultOptions(); 162 } 163 164 /** 165 * @see sfValidator 166 */ 167 public function getDefaultMessages() 168 { 169 return $this->validator->getDefaultMessages(); 162 170 } 163 171 } trunk/lib/validator/sfValidatorEmail.class.php
r5753 r5816 22 22 * @see sfValidatorRegex 23 23 */ 24 p ublic function __construct($options = array(), $messages = array())24 protected function configure($options = array(), $messages = array()) 25 25 { 26 $options['pattern'] = '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i'; 27 28 parent::__construct($options, $messages); 26 $this->setOption('pattern', '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i'); 29 27 } 30 28 } trunk/lib/validator/sfValidatorInteger.class.php
r5633 r5816 34 34 $this->setMessage('min', '"%value%" must be greater than %min%.'); 35 35 $this->setMessage('invalid', '"%value%" is not an integer.'); 36 37 $this->setOption('min', null); 38 $this->setOption('max', null); 36 39 } 37 40 … … 60 63 return $clean; 61 64 } 62 63 /**64 * @see sfValidator65 */66 public function getErrorCodes()67 {68 return array_merge(parent::getErrorCodes(), array('max', 'min'));69 }70 65 } trunk/lib/validator/sfValidatorNumber.class.php
r5581 r5816 34 34 $this->setMessage('min', '"%value%" is too short (smallest allowed is %min%).'); 35 35 $this->setMessage('invalid', '"%value%" is not a number.'); 36 37 $this->setOption('min', null); 38 $this->setOption('max', null); 36 39 } 37 40 … … 60 63 return $clean; 61 64 } 62 63 /**64 * @see sfValidator65 */66 public function getErrorCodes()67 {68 return array_merge(parent::getErrorCodes(), array('max', 'min'));69 }70 65 } trunk/lib/validator/sfValidatorRegex.class.php
r5753 r5816 20 20 { 21 21 /** 22 * Con structor.22 * Configures the current validator. 23 23 * 24 24 * Available options: 25 25 * 26 * * pattern: A regex pattern compatible with PCRE 26 * * pattern: A regex pattern compatible with PCRE (required) 27 27 * 28 28 * @see sfValidator 29 29 */ 30 p ublic function __construct($options = array(), $messages = array())30 protected function configure($options = array(), $messages = array()) 31 31 { 32 if (!isset($options['pattern'])) 33 { 34 throw new sfException('The "pattern" option is mandatory.'); 35 } 36 37 parent::__construct($options, $messages); 32 $this->addRequiredOption('pattern'); 38 33 } 39 34 … … 52 47 return $clean; 53 48 } 54 55 /**56 * @see sfValidator57 */58 protected function getOptionsWithoutDefaults()59 {60 return parent::getOptionsWithoutDefaults(array('pattern' => array('--fake--')));61 }62 63 /**64 * @see sfValidator65 */66 protected function getMessagesWithoutDefaults()67 {68 return parent::getMessagesWithoutDefaults(array('pattern' => array('--fake--')));69 }70 49 } trunk/lib/validator/sfValidatorSchema.class.php
r5753 r5816 70 70 public function configure($options = array(), $messages = array()) 71 71 { 72 $this-> options['allow_extra_fields'] = false;73 $this-> options['filter_extra_fields'] = true;74 75 $this-> messages['extra_fields'] = 'Extra field %field%.';72 $this->setOption('allow_extra_fields', false); 73 $this->setOption('filter_extra_fields', true); 74 75 $this->setMessage('extra_fields', 'Extra field %field%.'); 76 76 } 77 77 … … 190 190 191 191 /** 192 * @see sfValidator193 */194 public function getErrorCodes()195 {196 return array_merge(parent::getErrorCodes(), array('extra_fields'));197 }198 199 /**200 192 * Returns true if the schema has a field with the given name (implements the ArrayAccess interface). 201 193 * trunk/lib/validator/sfValidatorSchemaCompare.class.php
r5753 r5816 48 48 public function __construct($leftField, $operator, $rightField, $options = array(), $messages = array()) 49 49 { 50 $ options['leftField'] = $leftField;51 $ options['operator'] = $operator;52 $ options['rightField'] = $rightField;50 $this->setOption('left_field', $leftField); 51 $this->setOption('operator', $operator); 52 $this->setOption('right_field', $rightField); 53 53 54 54 parent::__construct(null, $options, $messages); 55 }56 57 /**58 * @see sfValidator59 */60 public function clean($value)61 {62 return $this->doClean($value);63 55 } 64 56 … … 78 70 } 79 71 80 $leftValue = isset($values[$this->getOption('left Field')]) ? $values[$this->getOption('leftField')] : null;81 $rightValue = isset($values[$this->getOption('right Field')]) ? $values[$this->getOption('rightField')] : null;72 $leftValue = isset($values[$this->getOption('left_field')]) ? $values[$this->getOption('left_field')] : null; 73 $rightValue = isset($values[$this->getOption('right_field')]) ? $values[$this->getOption('right_field')] : null; 82 74 83 75 switch ($this->getOption('operator')) … … 120 112 public function asString($indent = 0) 121 113 { 122 $options = $this->getOptionsWithoutDefaults( '--fake--', '--', '--fake--');123 $messages = $this->getMessagesWithoutDefaults( '--fake--', '--', '--fake--');124 unset($options['left Field'], $options['operator'], $options['rightField']);114 $options = $this->getOptionsWithoutDefaults(); 115 $messages = $this->getMessagesWithoutDefaults(); 116 unset($options['left_field'], $options['operator'], $options['right_field']); 125 117 126 118 $arguments = ''; … … 135 127 return sprintf('%s%s %s%s %s', 136 128 str_repeat(' ', $indent), 137 $this->getOption('left Field'),129 $this->getOption('left_field'), 138 130 $this->getOption('operator'), 139 131 $arguments, 140 $this->getOption('right Field')132 $this->getOption('right_field') 141 133 ); 142 134 } trunk/lib/validator/sfValidatorSchemaFilter.class.php
r5753 r5816 31 31 public function __construct($field, sfValidator $validator, $options = array(), $messages = array()) 32 32 { 33 $ options['field'] = $field;34 $ options['validator'] = $validator;33 $this->setOption('field', $field); 34 $this->setOption('validator', $validator); 35 35 36 36 parent::__construct(null, $options, $messages); 37 }38 39 /**40 * @see sfValidator41 */42 public function clean($value)43 {44 return $this->doClean($value);45 37 } 46 38 trunk/lib/validator/sfValidatorString.class.php
r5581 r5816 34 34 $this->setMessage('min_length', '"%value%" is too short (%min_length% characters min).'); 35 35 36 $this->setOption('max_length', null); 37 $this->setOption('min_length', null); 36 38 $this->setOption('empty_value', ''); 37 39 } … … 58 60 return $clean; 59 61 } 60 61 /**62 * @see sfValidator63 */64 public function getErrorCodes()65 {66 return array_merge(parent::getErrorCodes(), array('max_length', 'min_length'));67 }68 62 } trunk/lib/validator/sfValidatorUrl.class.php
r5753 r5816 22 22 * @see sfValidatorRegex 23 23 */ 24 public function __construct($options = array(), $messages = array())24 public function configure($options = array(), $messages = array()) 25 25 { 26 $ options['pattern'] ='~^26 $this->setOption('pattern', '~^ 27 27 https?:// # http or https 28 28 ( … … 33 33 (:[0-9]+)? # a port (optional) 34 34 (/?|/\S+) # a /, nothing or a / with something 35 $~ix'; 36 37 parent::__construct($options, $messages); 35 $~ix'); 38 36 } 39 37 } trunk/test/unit/validator/sfValidatorBooleanTest.php
r5634 r5816 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(1 7, new lime_output_color());13 $t = new lime_test(16, new lime_output_color()); 14 14 15 15 $v = new sfValidatorBoolean(); 16 17 // ->getErrorCodes()18 $t->diag('->getErrorCodes()');19 $t->is($v->getErrorCodes(), array('required', 'invalid'), '->getErrorCodes() returns all possible error codes');20 16 21 17 // ->clean() trunk/test/unit/validator/sfValidatorDateTest.php
r5581 r5816 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(1 6, new lime_output_color());13 $t = new lime_test(18, new lime_output_color()); 14 14 15 15 $v = new sfValidatorDate(); 16 17 // ->getErrorCodes()18 $t->diag('->getErrorCodes()');19 $t->is($v->getErrorCodes(), array('required', 'invalid', 'date_format'), '->getErrorCodes() returns all possible error codes');20 16 21 17 // ->clean() … … 43 39 $t->diag('validate timestamp'); 44 40 $t->is($v->clean(time()), date('Y-m-d', time()), '->clean() accepts timestamps as input'); 41 42 // validate date array 43 $t->diag('validate date array'); 44 $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15)), '2005-10-15', '->clean() accepts an array as an input'); 45 46 try 47 { 48 $v->clean(array('year' => -2, 'month' => 1, 'day' => 15)); 49 $t->fail('->clean() throws a sfValidatorError if the date is not valid'); 50 } 51 catch (sfValidatorError $e) 52 { 53 $t->pass('->clean() throws a sfValidatorError if the date is not valid'); 54 } 45 55 46 56 // validate regex … … 77 87 $t->diag('option with_time'); 78 88 $v->setOption('with_time', true); 89 $t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 12, 'minute' => 10, 'second' => 15)), '2005-10-15 12:10:15', '->clean() accepts an array as an input'); 79 90 $t->is($v->clean('18 october 2005 12:30'), '2005-10-18 12:30:00', '->clean() can accept date time with the with_time option'); 80 91 $t->is($v->clean(time()), date('Y-m-d H:i:s', time()), '->clean() can accept date time with the with_time option'); trunk/test/unit/validator/sfValidatorDecoratorTest.php
r5641 r5816 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(1 5, new lime_output_color());13 $t = new lime_test(18, new lime_output_color()); 14 14 15 15 class MyValidator extends sfValidatorDecorator 16 16 { 17 p rotectedfunction getValidator()17 public function getValidator() 18 18 { 19 19 return new sfValidatorString(array('min_length' => 2, 'trim' => true), array('required' => 'This string is required.')); … … 38 38 } 39 39 40 $v = new MyValidator(); 41 40 42 // ->getErrorCodes() 41 43 $t->diag('->getErrorCodes()'); 42 $t->is($v->getErrorCodes(), array('required', 'invalid', 'max_length', 'min_length'), '->getErrorCodes() returns error codes form the embedded validator'); 44 $t->is($v->getErrorCodes(), $v->getValidator()->getErrorCodes(), '->getErrorCodes() is a proxy to the embedded validator method'); 45 46 // ->asString() 47 $t->diag('->asString()'); 48 $t->is($v->asString(), $v->getValidator()->asString(), '->asString() is a proxy to the embedded validator method'); 49 50 // ->getDefaultMessages() 51 $t->diag('->getDefaultMessages()'); 52 $t->is($v->getDefaultMessages(), $v->getValidator()->getDefaultMessages(), '->getDefaultMessages() is a proxy to the embedded validator method'); 53 54 // ->getDefaultOptions() 55 $t->diag('->getDefaultOptions()'); 56 $t->is($v->getDefaultOptions(), $v->getValidator()->getDefaultOptions(), '->getDefaultOptions() is a proxy to the embedded validator method'); 43 57 44 58 // ->getMessage() ->getMessages() ->setMessage() ->setMessages() … … 55 69 $t->is($v->getOption('trim'), true, '->getOption() returns an option from the embedded validator'); 56 70 $v->setOption('trim', false); 57 $t->is($v->getOptions(), array('required' => true, 'trim' => false, 'empty_value' => '', 'min_length' => 2 ), '->getOptions() returns an array of options from the embedded validator');71 $t->is($v->getOptions(), array('required' => true, 'trim' => false, 'empty_value' => '', 'min_length' => 2, 'max_length' => null), '->getOptions() returns an array of options from the embedded validator'); 58 72 $t->is($v->hasOption('min_length'), true, '->hasOption() returns true if the embedded validator has a given option'); 59 73 $v->setOptions(array('min_length' => 10)); trunk/test/unit/validator/sfValidatorFromDescriptionTest.php
r5757 r5816 104 104 105 105 'email:Email and (age:Integer({min: 18}) or (age:Integer({max: 18}) and is_young:Boolean({required: true})))', 106 '(password == password_bis) and begin_date <= end_date and password:String({min : 4, max: 18})',107 'countries:Choice({choices: [France, USA, Italy, Spain]}) and password ==({ invalid: "Passwords must be the same (%left_field% != %right_field%)"}) password_bis and begin_date <= end_date and password:String({min: 4, max: 18})',106 '(password == password_bis) and begin_date <= end_date and password:String({min_length: 4, max_length: 18})',&nb