Commit d3098e3a authored by doctrine's avatar doctrine

Minor validator fix

parent 7b19bef1
...@@ -312,8 +312,10 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -312,8 +312,10 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
* @return boolean * @return boolean
*/ */
public function remove($key) { public function remove($key) {
if( ! isset($this->data[$key])) if( ! isset($this->data[$key])) {
$this->expand($key);
throw new InvalidKeyException(); throw new InvalidKeyException();
}
$removed = $this->data[$key]; $removed = $this->data[$key];
...@@ -417,7 +419,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -417,7 +419,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
if(in_array($record,$this->data)) { if(in_array($record,$this->data)) {
return false; return false;
} else }
if(isset($this->generator)) { if(isset($this->generator)) {
$key = $this->generator->getIndex($record); $key = $this->generator->getIndex($record);
......
...@@ -21,25 +21,24 @@ class Doctrine_Form implements Iterator { ...@@ -21,25 +21,24 @@ class Doctrine_Form implements Iterator {
$definitions = $this->columns[$column]; $definitions = $this->columns[$column];
$e = explode("|",$definitions[2]); $e = explode("|",$definitions[2]);
$enum = false; $enum = false;
foreach($e as $v) {
$e2 = explode(":",$v); if($definitions[0] == "enum")
if($e2[0] == "enum") { $enum = $this->record->getTable()->getEnumValues($column);
$enum = explode("-",$e2[1]);
break;
}
}
$length = $definitions[1]; $length = $definitions[1];
if( ! in_array("autoincrement",$e) && ! in_array("protected",$e)) { if( ! in_array("autoincrement",$e) && ! in_array("protected",$e)) {
if($enum) { if($enum) {
$elements[$column] = "<select name='data[$column]'>\n"; $elements[$column] = "<select name='data[$column]'>\n";
foreach($enum as $k => $v) { foreach($enum as $k => $v) {
if($this->record->get($column) == $k) { if($this->record->get($column) == $v) {
$str = 'selected'; $str = 'selected';
} else } else
$str = ''; $str = '';
$elements[$column] .= " <option value='$k' $str>$v</option>\n"; $elements[$column] .= " <option value='$v' $str>$v</option>\n";
} }
$elements[$column] .= "</select>\n"; $elements[$column] .= "</select>\n";
} else { } else {
......
...@@ -29,7 +29,7 @@ class Doctrine_Lib { ...@@ -29,7 +29,7 @@ class Doctrine_Lib {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @return string * @return string
*/ */
public function getRecordAsString(Doctrine_Record $record) { public static function getRecordAsString(Doctrine_Record $record) {
$r[] = "<pre>"; $r[] = "<pre>";
$r[] = "Component : ".$record->getTable()->getComponentName(); $r[] = "Component : ".$record->getTable()->getComponentName();
$r[] = "ID : ".$record->getID(); $r[] = "ID : ".$record->getID();
...@@ -65,7 +65,7 @@ class Doctrine_Lib { ...@@ -65,7 +65,7 @@ class Doctrine_Lib {
* @param Doctrine_Session $session * @param Doctrine_Session $session
* @return string * @return string
*/ */
public function getSessionAsString(Doctrine_Session $session) { public static function getSessionAsString(Doctrine_Session $session) {
$r[] = "<pre>"; $r[] = "<pre>";
$r[] = "Doctrine_Session object"; $r[] = "Doctrine_Session object";
$r[] = "State : ".Doctrine_Lib::getSessionStateAsString($session->getState()); $r[] = "State : ".Doctrine_Lib::getSessionStateAsString($session->getState());
...@@ -109,7 +109,7 @@ class Doctrine_Lib { ...@@ -109,7 +109,7 @@ class Doctrine_Lib {
* @param Doctrine_Table $table * @param Doctrine_Table $table
* @return string * @return string
*/ */
public function getTableAsString(Doctrine_Table $table) { public static function getTableAsString(Doctrine_Table $table) {
$r[] = "<pre>"; $r[] = "<pre>";
$r[] = "Component : ".$this->getComponentName(); $r[] = "Component : ".$this->getComponentName();
$r[] = "Table : ".$this->getTableName(); $r[] = "Table : ".$this->getTableName();
...@@ -124,7 +124,7 @@ class Doctrine_Lib { ...@@ -124,7 +124,7 @@ class Doctrine_Lib {
/** /**
* @return string * @return string
*/ */
public function formatSql($sql) { public static function formatSql($sql) {
$e = explode("\n",$sql); $e = explode("\n",$sql);
$color = "367FAC"; $color = "367FAC";
$l = $sql; $l = $sql;
...@@ -146,7 +146,7 @@ class Doctrine_Lib { ...@@ -146,7 +146,7 @@ class Doctrine_Lib {
* @param Doctrine_Collection $collection * @param Doctrine_Collection $collection
* @return string * @return string
*/ */
public function getCollectionAsString(Doctrine_Collection $collection) { public static function getCollectionAsString(Doctrine_Collection $collection) {
$r[] = "<pre>"; $r[] = "<pre>";
$r[] = get_class($collection); $r[] = get_class($collection);
......
...@@ -172,6 +172,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -172,6 +172,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
// listen the onLoad event // listen the onLoad event
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onLoad($this); $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onLoad($this);
} }
$this->table->getRepository()->add($this); $this->table->getRepository()->add($this);
} }
} }
...@@ -229,7 +230,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -229,7 +230,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if( ! isset($tmp[$name])) { if( ! isset($tmp[$name])) {
if($type == 'array') { if($type == 'array') {
$this->data[$name] = array(); $this->data[$name] = array();
$this->modified[] = $name;
} else } else
$this->data[$name] = self::$null; $this->data[$name] = self::$null;
} else { } else {
...@@ -715,7 +715,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -715,7 +715,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
final public function getModified() { final public function getModified() {
$a = array(); $a = array();
foreach($this->modified as $k=>$v) { foreach($this->modified as $k => $v) {
$a[$v] = $this->data[$v]; $a[$v] = $this->data[$v];
} }
return $a; return $a;
...@@ -726,10 +726,13 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -726,10 +726,13 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* *
* @return array * @return array
*/ */
final public function getPrepared() { final public function getPrepared(array $array = array()) {
$a = array(); $a = array();
foreach($this->modified as $k => $v) { if(empty($array))
$array = $this->modified;
foreach($array as $k => $v) {
$type = $this->table->getTypeOf($v); $type = $this->table->getTypeOf($v);
if($type == 'array' || if($type == 'array' ||
......
...@@ -127,12 +127,9 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -127,12 +127,9 @@ class Doctrine_Table extends Doctrine_Configurable {
$record = new $name($this); $record = new $name($this);
$names = array(); $names = array();
$class = $name; $class = $name;
// get parent classes // get parent classes
...@@ -825,6 +822,16 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -825,6 +822,16 @@ class Doctrine_Table extends Doctrine_Configurable {
final public function setEnumValues($field, array $values) { final public function setEnumValues($field, array $values) {
$this->enum[$field] = $values; $this->enum[$field] = $values;
} }
/**
* @param string $field
* @return array
*/
final public function getEnumValues($field) {
if(isset($this->enum[$field]))
return $this->enum[$field];
else
return array();
}
/** /**
* enumValue * enumValue
*/ */
...@@ -836,7 +843,7 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -836,7 +843,7 @@ class Doctrine_Table extends Doctrine_Configurable {
*/ */
final public function enumIndex($field, $value) { final public function enumIndex($field, $value) {
$v = array_search($value, $this->enum[$field]); $v = array_search($value, $this->enum[$field]);
return ($v !== false)?$v:$value; return $v;
} }
/** /**
* @return integer * @return integer
......
...@@ -106,21 +106,31 @@ class Doctrine_Validator { ...@@ -106,21 +106,31 @@ class Doctrine_Validator {
switch($record->getState()): switch($record->getState()):
case Doctrine_Record::STATE_TDIRTY: case Doctrine_Record::STATE_TDIRTY:
case Doctrine_Record::STATE_TCLEAN: case Doctrine_Record::STATE_TCLEAN:
// all fields will be validated
$data = $record->getData(); $data = $record->getData();
break; break;
default: default:
// only the modified fields will be validated
$data = $record->getModified(); $data = $record->getModified();
endswitch; endswitch;
$err = array(); $err = array();
foreach($data as $key => $value) { foreach($data as $key => $value) {
if($value === self::$null) if($value === self::$null)
$value = null; $value = null;
$column = $columns[$key]; $column = $columns[$key];
if($column[0] == "enum") {
$value = $record->getTable()->enumIndex($value);
if($value === false) {
$err[$key] = Doctrine_Validator::ERR_ENUM;
continue;
}
}
if($column[0] == 'array' || $column[0] == 'object') if($column[0] == "array" || $column[0] == "object")
$length = strlen(serialize($value)); $length = strlen(serialize($value));
else else
$length = strlen($value); $length = strlen($value);
...@@ -199,6 +209,9 @@ class Doctrine_Validator { ...@@ -199,6 +209,9 @@ class Doctrine_Validator {
*/ */
public static function isValidType($var, $type) { public static function isValidType($var, $type) {
$looseType = self::gettype($var); $looseType = self::gettype($var);
if($type == 'enum')
$type = 'integer';
switch($looseType): switch($looseType):
case 'float': case 'float':
case 'double': case 'double':
......
...@@ -158,5 +158,6 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { ...@@ -158,5 +158,6 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($a["User"][0]["name"], Doctrine_Validator::ERR_LENGTH); $this->assertEqual($a["User"][0]["name"], Doctrine_Validator::ERR_LENGTH);
$this->manager->setAttribute(Doctrine::ATTR_VLD, false); $this->manager->setAttribute(Doctrine::ATTR_VLD, false);
} }
} }
?> ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment