Changeset 4045
- Timestamp:
- 03/19/08 14:30:21 (16 months ago)
- Location:
- branches/0.10
- Files:
-
- 5 modified
-
lib/Doctrine/Query.php (modified) (2 diffs)
-
lib/Doctrine/Relation/Association.php (modified) (1 diff)
-
lib/Doctrine/Table.php (modified) (1 diff)
-
lib/Doctrine/Validator.php (modified) (10 diffs)
-
tests/ValidatorTestCase.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/0.10/lib/Doctrine/Query.php
r3983 r4045 763 763 764 764 // build sql expression 765 if ($this->_conn instanceof Doctrine_Connection_Oracle) { 766 $tableAlias = $this->_queryComponents[$componentAlias]['table'] 767 ->getTableName(); 768 } 765 769 $term[0] = $this->_conn->quoteIdentifier($tableAlias) 766 770 . '.' … … 803 807 if ($this->getType() === Doctrine_Query::SELECT) { 804 808 // build sql expression 809 if ($this->_conn instanceof Doctrine_Connection_Oracle) { 810 $tableAlias = $this->_queryComponents[$componentAlias]['table'] 811 ->getTableName(); 812 } 805 813 $term[0] = $this->_conn->quoteIdentifier($tableAlias) 806 814 . '.' -
branches/0.10/lib/Doctrine/Relation/Association.php
r3884 r4045 91 91 $coll = new Doctrine_Collection($this->getTable()); 92 92 } else { 93 $coll = Doctrine_Query::create()->query($this->getRelationDql(1), array($id));93 $coll = $this->getTable()->getConnection()->query($this->getRelationDql(1), array($id)); 94 94 } 95 95 $coll->setReference($record, $this); -
branches/0.10/lib/Doctrine/Table.php
r3977 r4045 1858 1858 1859 1859 /** 1860 * Gets the names of all validators that are applied on a field. 1861 * 1862 * @param string The field name. 1863 * @return array The names of all validators that are applied on the specified field. 1864 */ 1865 public function getFieldValidators($fieldName) 1866 { 1867 $validators = array(); 1868 $columnName = $this->getColumnName($fieldName); 1869 // this loop is a dirty workaround to get the validators filtered out of 1870 // the options, since everything is squeezed together currently 1871 foreach ($this->_columns[$columnName] as $name => $args) { 1872 if (empty($name) 1873 || $name == 'primary' 1874 || $name == 'protected' 1875 || $name == 'autoincrement' 1876 || $name == 'default' 1877 || $name == 'values' 1878 || $name == 'sequence' 1879 || $name == 'zerofill' 1880 || $name == 'owner' 1881 || $name == 'scale' 1882 || $name == 'type' 1883 || $name == 'length') { 1884 continue; 1885 } 1886 if (strtolower($name) === 'notnull' && isset($this->_columns[$columnName]['autoincrement'])) { 1887 continue; 1888 } 1889 $validators[$name] = $args; 1890 } 1891 1892 return $validators; 1893 } 1894 1895 /** 1896 * Gets the (maximum) length of a field. 1897 */ 1898 public function getFieldLength($fieldName) 1899 { 1900 return $this->_columns[$this->getColumnName($fieldName)]['length']; 1901 } 1902 1903 /** 1860 1904 * getBoundQueryPart 1861 1905 * -
branches/0.10/lib/Doctrine/Validator.php
r3975 r4045 22 22 /** 23 23 * Doctrine_Validator 24 * 25 * Doctrine_Validator performs validations in record properties 24 * Doctrine_Validator performs validations on record properties 26 25 * 27 26 * @package Doctrine … … 31 30 * @since 1.0 32 31 * @version $Revision$ 32 * @author Roman Borschel <roman@code-factory.org> 33 33 * @author Konsta Vesterinen <kvesteri@cc.hut.fi> 34 34 */ … … 38 38 * @var array $validators an array of validator objects 39 39 */ 40 private static $validators = array();40 private static $validators = array(); 41 41 42 42 /** … … 52 52 if (class_exists($class)) { 53 53 self::$validators[$name] = new $class; 54 } else if (class_exists($name)) { 55 self::$validators[$name] = new $name; 54 56 } else { 55 57 throw new Doctrine_Exception("Validator named '$name' not available."); … … 69 71 public function validateRecord(Doctrine_Record $record) 70 72 { 71 $columns = $record->getTable()->getColumns(); 72 $component = $record->getTable()->getComponentName(); 73 $table = $record->getTable(); 74 $columns = $table->getColumns(); 75 $component = $table->getComponentName(); 73 76 74 77 $errorStack = $record->getErrorStack(); … … 76 79 // if record is transient all fields will be validated 77 80 // if record is persistent only the modified fields will be validated 78 $data = ($record->exists()) ? $record->getModified() : $record->getData(); 79 80 $err = array(); 81 foreach ($data as $key => $value) { 81 $fields = ($record->exists()) ? $record->getModified() : $record->getData(); 82 $err = array(); 83 foreach ($fields as $fieldName => $value) { 82 84 if ($value === self::$_null) { 83 85 $value = null; … … 85 87 $value = $value->getIncremented(); 86 88 } 87 88 $column = $columns[$record->getTable()->getColumnName($key)]; 89 90 if ($column['type'] == 'enum') { 91 $value = $record->getTable()->enumIndex($key, $value); 92 93 if ($value === false) { 94 $errorStack->add($key, 'enum'); 95 continue; 96 } 97 } 98 99 if ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS) { 100 if ( ! $this->validateLength($column, $key, $value)) { 101 $errorStack->add($key, 'length'); 102 103 continue; 104 } 105 } 106 107 foreach ($column as $name => $args) { 108 if (empty($name) 109 || $name == 'primary' 110 || $name == 'protected' 111 || $name == 'autoincrement' 112 || $name == 'default' 113 || $name == 'values' 114 || $name == 'sequence' 115 || $name == 'zerofill' 116 || $name == 'owner' 117 || $name == 'scale') { 118 continue; 119 } 120 121 if (strtolower($name) === 'notnull' && isset($column['autoincrement'])) { 122 continue; 123 } 124 125 if (strtolower($name) == 'length') { 126 if ( ! ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS)) { 127 if ( ! $this->validateLength($column, $key, $value)) { 128 $errorStack->add($key, 'length'); 129 } 89 90 $dataType = $table->getTypeOf($fieldName); 91 92 // Validate field type, if type validation is enabled 93 if ($table->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES) { 94 if ( ! self::isValidType($value, $dataType)) { 95 $errorStack->add($fieldName, 'type'); 96 } 97 if ($dataType == 'enum') { 98 $enumIndex = $table->enumIndex($fieldName, $value); 99 if ($enumIndex === false) { 100 $errorStack->add($fieldName, 'enum'); 130 101 } 131 continue; 132 } 133 134 if (strtolower($name) == 'type') { 135 if ( ! ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES)) { 136 if ( ! self::isValidType($value, $column['type'])) { 137 $errorStack->add($key, 'type'); 138 } 139 } 140 continue; 141 } 142 143 $validator = self::getValidator($name); 102 } 103 } 104 105 // Validate field length, if length validation is enabled 106 if ($table->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS) { 107 if ( ! $this->validateLength($value, $dataType, $table->getFieldLength($fieldName))) { 108 $errorStack->add($fieldName, 'length'); 109 } 110 } 111 112 // Run all custom validators 113 foreach ($table->getFieldValidators($fieldName) as $validatorName => $args) { 114 if ( ! is_string($validatorName)) { 115 $validatorName = $args; 116 $args = array(); 117 } 118 $validator = self::getValidator($validatorName); 144 119 $validator->invoker = $record; 145 $validator->field = $key; 146 $validator->args = $args; 147 120 $validator->field = $fieldName; 121 $validator->args = $args; 148 122 if ( ! $validator->validate($value)) { 149 $errorStack->add($key, $name); 150 151 //$err[$key] = 'not valid'; 152 153 // errors found quit validation looping for this column 154 //break; 155 } 156 } 157 158 if ($record->getTable()->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES) { 159 if ( ! self::isValidType($value, $column['type'])) { 160 $errorStack->add($key, 'type'); 161 continue; 123 $errorStack->add($fieldName, $validatorName); 162 124 } 163 125 } … … 168 130 * Validates the length of a field. 169 131 */ 170 private function validateLength($column, $key, $value) 171 { 172 if ($column['type'] == 'timestamp' || $column['type'] == 'integer' || 173 $column['type'] == 'enum') { 132 private function validateLength($value, $type, $maximumLength) 133 { 134 if ($type == 'timestamp' || $type == 'integer' || $type == 'enum') { 174 135 return true; 175 } else if ($ column['type'] == 'array' || $column['type']== 'object') {136 } else if ($type == 'array' || $type == 'object') { 176 137 $length = strlen(serialize($value)); 177 138 } else { 178 139 $length = strlen($value); 179 140 } 180 181 if ($length > $column['length']) { 141 if ($length > $maximumLength) { 182 142 return false; 183 143 } … … 193 153 { 194 154 return (count($this->stack) > 0); 195 } 196 197 /** 198 * phpType 199 * converts a doctrine type to native php type 200 * 201 * @param $portableType portable doctrine type 202 * @return string 203 *//* 204 public static function phpType($portableType) 205 { 206 switch ($portableType) { 207 case 'enum': 208 return 'integer'; 209 case 'blob': 210 case 'clob': 211 case 'mbstring': 212 case 'timestamp': 213 case 'date': 214 case 'gzip': 215 return 'string'; 216 break; 217 default: 218 return $portableType; 219 } 220 }*/ 221 /** 222 * returns whether or not the given variable is 223 * valid type 224 * 225 * @param mixed $var 226 * @param string $type 227 * @return boolean 228 */ 229 /* 230 public static function isValidType($var, $type) 231 { 232 if ($type == 'boolean') { 233 return true; 234 } 235 236 $looseType = self::gettype($var); 237 $type = self::phpType($type); 238 239 switch ($looseType) { 240 case 'float': 241 case 'double': 242 case 'integer': 243 if ($type == 'string' || $type == 'float') { 244 return true; 245 } 246 case 'string': 247 case 'array': 248 case 'object': 249 return ($type === $looseType); 250 break; 251 case 'NULL': 252 return true; 253 break; 254 } 255 }*/ 256 155 } 257 156 258 157 /** … … 302 201 } 303 202 } 304 305 306 /**307 * returns the type of loosely typed variable308 *309 * @param mixed $var310 * @return string311 *//*312 public static function gettype($var)313 {314 $type = gettype($var);315 switch ($type) {316 case 'string':317 if (preg_match("/^[0-9]+$/",$var)) {318 return 'integer';319 } elseif (is_numeric($var)) {320 return 'float';321 } else {322 return $type;323 }324 break;325 default:326 return $type;327 }328 }*/329 203 } -
branches/0.10/tests/ValidatorTestCase.php
r3884 r4045 113 113 } 114 114 115 /*public function testDecimalType() 116 { 117 $validDecimals = array('99.22', 99999.3); 118 foreach ($validDecimals as $value) { 119 $this->assertTrue(Doctrine_Validator::isValidType($value, 'decimal')); 120 } 121 122 $invalidDecimals = array('decimal', '99999999', '99999999.3', '0.0002', 0.001); 123 foreach ($invalidDecimals as $value) { 124 $this->assertFalse(Doctrine_Validator::isValidType($value, 'decimal')); 125 } 126 127 }*/ 115 128 116 129 public function testValidate2() … … 140 153 public function testValidate() 141 154 { 155 $this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL); 142 156 $user = $this->connection->getTable('User')->find(4); 143 157 … … 172 186 173 187 $this->assertTrue(in_array('unique', $stack['address'])); 188 $this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_NONE); 174 189 } 175 190 … … 383 398 $s = $dve->getInvalidRecords(); 384 399 $this->assertEqual(1, count($dve->getInvalidRecords())); 400 $invalids = $dve->getInvalidRecords(); 401 //var_dump($invalids[0]->getErrorStack()); 402 //echo "<br/><br/>"; 403 //var_dump($invalids[1]->getErrorStack()); 385 404 $stack = $client->ValidatorTest_AddressModel[0]->getErrorStack(); 386 405