Changeset 5882
- Timestamp:
- 11/06/07 20:58:15 (2 years ago)
- Files:
-
- trunk/lib/validator/sfValidator.class.php (modified) (7 diffs)
- trunk/lib/validator/sfValidatorBoolean.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorCallback.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorDate.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorEmail.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorInteger.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorNumber.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorSchema.class.php (modified) (2 diffs)
- trunk/lib/validator/sfValidatorSchemaCompare.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorSchemaFilter.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorString.class.php (modified) (1 diff)
- trunk/lib/validator/sfValidatorUrl.class.php (modified) (1 diff)
- trunk/test/unit/validator/sfValidatorChoiceManyTest.php (modified) (2 diffs)
- trunk/test/unit/validator/sfValidatorTest.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/validator/sfValidator.class.php
r5816 r5882 40 40 * * empty_value: empty value when value is not required 41 41 * 42 * Available error codes: 43 * 44 * * required 45 * * invalid 46 * 42 47 * @param array An array of options 43 48 * @param array An array of error messages … … 66 71 67 72 // check required options 68 if ($diff = array_diff($this->requiredOptions, array_ keys($options)))73 if ($diff = array_diff($this->requiredOptions, array_merge(array_keys($this->options), array_keys($options)))) 69 74 { 70 75 throw new sfException(sprintf('%s requires the following options: \'%s\'.', get_class($this), implode('\', \'', $diff))); … … 107 112 108 113 /** 109 * Changes an error message given the error code.114 * Adds a new error code with a default error message. 110 115 * 111 116 * @param string The error code 112 117 * @param string The error message 113 118 */ 119 public function addMessage($name, $value) 120 { 121 $this->messages[$name] = $value; 122 } 123 124 /** 125 * Changes an error message given the error code. 126 * 127 * @param string The error code 128 * @param string The error message 129 */ 114 130 public function setMessage($name, $value) 115 131 { 132 if (!in_array($name, array_keys($this->messages))) 133 { 134 throw new sfException(sprintf('%s does not support the following error code: \'%s\'.', get_class($this), $name)); 135 } 136 116 137 $this->messages[$name] = $value; 117 138 } … … 147 168 { 148 169 return isset($this->options[$name]) ? $this->options[$name] : null; 170 } 171 172 /** 173 * Adds a new option value with a default value. 174 * 175 * @param string The option name 176 * @param mixed The default value 177 */ 178 public function addOption($name, $value = null) 179 { 180 $this->options[$name] = $value; 149 181 } 150 182 … … 157 189 public function setOption($name, $value) 158 190 { 191 if (!in_array($name, array_merge(array_keys($this->options), $this->requiredOptions))) 192 { 193 throw new sfException(sprintf('%s does not support the following option: \'%s\'.', get_class($this), $name)); 194 } 195 159 196 $this->options[$name] = $value; 160 197 } … … 190 227 { 191 228 $this->options = $values; 229 } 230 231 /** 232 * Adds a required option. 233 * 234 * @param string The option name 235 */ 236 public function addRequiredOption($name) 237 { 238 $this->requiredOptions[] = $name; 239 } 240 241 /** 242 * Returns all required option names. 243 * 244 * @param array An array of required option names 245 */ 246 public function getRequiredOptions() 247 { 248 return $this->requiredOptions; 192 249 } 193 250 … … 333 390 { 334 391 $this->defaultOptions = $options; 335 }336 337 /**338 * Adds a required option.339 *340 * @param string The option name341 */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 names351 */352 public function getRequiredOptions()353 {354 return $this->requiredOptions;355 392 } 356 393 trunk/lib/validator/sfValidatorBoolean.class.php
r5816 r5882 31 31 protected function configure($options = array(), $messages = array()) 32 32 { 33 $this->setOption('true_values', array('true', 't', 'yes', 'y', 'on', '1')); 34 $this->setOption('false_values', array('false', 'f', 'no', 'n', 'off', '0')); 33 $this->addOption('true_values', array('true', 't', 'yes', 'y', 'on', '1')); 34 $this->addOption('false_values', array('false', 'f', 'no', 'n', 'off', '0')); 35 35 36 $this->setOption('required', false); 36 37 } trunk/lib/validator/sfValidatorCallback.class.php
r5816 r5882 31 31 { 32 32 $this->addRequiredOption('callback'); 33 33 34 $this->setOption('required', false); 34 35 } trunk/lib/validator/sfValidatorDate.class.php
r5816 r5882 24 24 * Available options: 25 25 * 26 * * date_format: A regular expression that dates must match 27 * * with_time: true if the validator must return a time, false otherwise 28 * * date_output: The format to use when returning a date (default to Y-m-d) 29 * * datetime_output: The format to use when returning a date with time (default to Y-m-d H:i:s) 26 * * date_format: A regular expression that dates must match 27 * * with_time: true if the validator must return a time, false otherwise 28 * * date_output: The format to use when returning a date (default to Y-m-d) 29 * * datetime_output: The format to use when returning a date with time (default to Y-m-d H:i:s) 30 * * date_format_error: The date format to use when displaying an error for a bad_format error 31 * 32 * Available error codes: 33 * 34 * * bad_format 30 35 * 31 36 * @see sfValidator … … 33 38 protected function configure($options = array(), $messages = array()) 34 39 { 35 $this-> setMessage('bad_format', '"%value%" does not match the date format (%date_format%).');40 $this->addMessage('bad_format', '"%value%" does not match the date format (%date_format%).'); 36 41 37 $this->setOption('date_format', null); 38 $this->setOption('with_time', false); 39 $this->setOption('date_output', 'Y-m-d'); 40 $this->setOption('datetime_output', 'Y-m-d H:i:s'); 42 $this->addOption('date_format', null); 43 $this->addOption('with_time', false); 44 $this->addOption('date_output', 'Y-m-d'); 45 $this->addOption('datetime_output', 'Y-m-d H:i:s'); 46 $this->addOption('date_format_error'); 41 47 } 42 48 trunk/lib/validator/sfValidatorEmail.class.php
r5816 r5882 24 24 protected function configure($options = array(), $messages = array()) 25 25 { 26 parent::configure($options, $messages); 27 26 28 $this->setOption('pattern', '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i'); 27 29 } trunk/lib/validator/sfValidatorInteger.class.php
r5816 r5882 27 27 * * min: The minimum value allowed 28 28 * 29 * Available error codes: 30 * 31 * * max 32 * * min 33 * 29 34 * @see sfValidator 30 35 */ 31 36 protected function configure($options = array(), $messages = array()) 32 37 { 33 $this->setMessage('max', '"%value%" must be less than %max%.'); 34 $this->setMessage('min', '"%value%" must be greater than %min%.'); 38 $this->addMessage('max', '"%value%" must be less than %max%.'); 39 $this->addMessage('min', '"%value%" must be greater than %min%.'); 40 41 $this->addOption('min'); 42 $this->addOption('max'); 43 35 44 $this->setMessage('invalid', '"%value%" is not an integer.'); 36 37 $this->setOption('min', null);38 $this->setOption('max', null);39 45 } 40 46 trunk/lib/validator/sfValidatorNumber.class.php
r5816 r5882 27 27 * * min: The minimum value allowed 28 28 * 29 * Available error codes: 30 * 31 * * max 32 * * min 33 * 29 34 * @see sfValidator 30 35 */ 31 36 protected function configure($options = array(), $messages = array()) 32 37 { 33 $this->setMessage('max', '"%value%" is too long (largest allowed is %max%).'); 34 $this->setMessage('min', '"%value%" is too short (smallest allowed is %min%).'); 38 $this->addMessage('max', '"%value%" is too long (largest allowed is %max%).'); 39 $this->addMessage('min', '"%value%" is too short (smallest allowed is %min%).'); 40 41 $this->addOption('min'); 42 $this->addOption('max'); 43 35 44 $this->setMessage('invalid', '"%value%" is not a number.'); 36 37 $this->setOption('min', null);38 $this->setOption('max', null);39 45 } 40 46 trunk/lib/validator/sfValidatorSchema.class.php
r5816 r5882 63 63 * * filter_extra_fields: if true, the validator filters extra fields from the returned array of cleaned values (default to true) 64 64 * 65 * Available error codes: 66 * 67 * * extra_fields 68 * 65 69 * @param array An array of options 66 70 * @param array An array of error messages … … 70 74 public function configure($options = array(), $messages = array()) 71 75 { 72 $this-> setOption('allow_extra_fields', false);73 $this-> setOption('filter_extra_fields', true);74 75 $this-> setMessage('extra_fields', 'Extra field %field%.');76 $this->addOption('allow_extra_fields', false); 77 $this->addOption('filter_extra_fields', true); 78 79 $this->addMessage('extra_fields', 'Extra field %field%.'); 76 80 } 77 81 trunk/lib/validator/sfValidatorSchemaCompare.class.php
r5816 r5882 48 48 public function __construct($leftField, $operator, $rightField, $options = array(), $messages = array()) 49 49 { 50 $this-> setOption('left_field', $leftField);51 $this-> setOption('operator', $operator);52 $this-> setOption('right_field', $rightField);50 $this->addOption('left_field', $leftField); 51 $this->addOption('operator', $operator); 52 $this->addOption('right_field', $rightField); 53 53 54 54 parent::__construct(null, $options, $messages); trunk/lib/validator/sfValidatorSchemaFilter.class.php
r5816 r5882 31 31 public function __construct($field, sfValidator $validator, $options = array(), $messages = array()) 32 32 { 33 $this-> setOption('field', $field);34 $this-> setOption('validator', $validator);33 $this->addOption('field', $field); 34 $this->addOption('validator', $validator); 35 35 36 36 parent::__construct(null, $options, $messages); trunk/lib/validator/sfValidatorString.class.php
r5816 r5882 27 27 * * min_length: The minimum length of the string 28 28 * 29 * Available error codes: 30 * 31 * * max_length 32 * * min_length 33 * 29 34 * @see sfValidator 30 35 */ 31 36 protected function configure($options = array(), $messages = array()) 32 37 { 33 $this-> setMessage('max_length', '"%value%" is too long (%max_length% characters max).');34 $this-> setMessage('min_length', '"%value%" is too short (%min_length% characters min).');38 $this->addMessage('max_length', '"%value%" is too long (%max_length% characters max).'); 39 $this->addMessage('min_length', '"%value%" is too short (%min_length% characters min).'); 35 40 36 $this->setOption('max_length', null); 37 $this->setOption('min_length', null); 41 $this->addOption('max_length'); 42 $this->addOption('min_length'); 43 38 44 $this->setOption('empty_value', ''); 39 45 } trunk/lib/validator/sfValidatorUrl.class.php
r5816 r5882 24 24 public function configure($options = array(), $messages = array()) 25 25 { 26 parent::configure($options, $messages); 27 26 28 $this->setOption('pattern', '~^ 27 29 https?:// # http or https trunk/test/unit/validator/sfValidatorChoiceManyTest.php
r5757 r5882 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 5, new lime_output_color());13 $t = new lime_test(6, new lime_output_color()); 14 14 15 15 $v = new sfValidatorChoiceMany(array('choices' => array('foo', 'bar'))); … … 40 40 $t->pass('->clean() throws an sfValidatorError if the value is not an expected value'); 41 41 } 42 43 function choice_callable() 44 { 45 return array(1, 2, 3); 46 } 47 48 // choices as a callable 49 $t->diag('choices as a callable'); 50 $v = new sfValidatorChoiceMany(array('choices' => new sfCallable('choice_callable'))); 51 $t->is($v->clean(array('2')), array('2'), '__construct() can take a sfCallable object as a choices option'); trunk/test/unit/validator/sfValidatorTest.php
r5816 r5882 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(3 3, new lime_output_color());13 $t = new lime_test(37, new lime_output_color()); 14 14 15 15 class ValidatorIdentity extends sfValidator … … 17 17 protected function configure($options = array(), $messages = array()) 18 18 { 19 $this-> setOption('foo', 'bar');20 $this-> setMessage('foo', 'bar');19 $this->addOption('foo', 'bar'); 20 $this->addMessage('foo', 'bar'); 21 21 } 22 22 … … 75 75 } 76 76 77 // ->getRequiredOptions() 77 78 $t->diag('getRequiredOptions'); 78 79 $v = new ValidatorIdentityWithRequired(array('foo' => 'bar')); … … 118 119 $v->setOption('trim', true); 119 120 $t->is($v->clean(' foo '), 'foo', '->setOption() can turn on whitespace trimming'); 121 try 122 { 123 $v->setOption('foobar', 'foo'); 124 $t->fail('->setOption() throws an sfException if the option is not registered'); 125 } 126 catch (sfException $e) 127 { 128 $t->pass('->setOption() throws an sfException if the option is not registered'); 129 } 120 130 121 131 // ->hasOption() … … 129 139 $t->is($v->getOption('nonexistant'), null, '->getOption() returns null if the option does not exist'); 130 140 141 // ->addOption() 142 $t->diag('->addOption()'); 143 $v->addOption('foobar'); 144 $v->setOption('foobar', 'foo'); 145 $t->is($v->getOption('foobar'), 'foo', '->addOption() adds a new option to a validator'); 146 131 147 // ->getOptions() ->setOptions() 132 148 $t->diag('->getOptions() ->setOptions()'); … … 156 172 } 157 173 174 try 175 { 176 $v->setMessage('foobar', 'foo'); 177 $t->fail('->setMessage() throws an sfException if the message is not registered'); 178 } 179 catch (sfException $e) 180 { 181 $t->pass('->setMessage() throws an sfException if the message is not registered'); 182 } 183 158 184 // ->setMessages() 159 185 $t->diag('->setMessages()'); 160 186 $v->setMessages(array('required' => 'This is required.')); 161 187 $t->is($v->getMessages(), array('required' => 'This is required.'), '->setMessages() changes all error messages'); 188 189 // ->addMessage() 190 $t->diag('->addMessage()'); 191 $v->addMessage('foobar', 'foo'); 192 $v->setMessage('foobar', 'bar'); 193 $t->is($v->getMessage('foobar'), 'bar', '->addMessage() adds a new error code'); 162 194 163 195 // ->getErrorCodes()

