Commit 4433e6ff authored by romanb's avatar romanb

Introduced constants: Doctrine::ATTR_AUTO_LENGTH_VLD and Doctrine::ATTR_AUTO_TYPE_VLD.

Both default to TRUE. If set to false, you need to explicitly specify "length" and/or "type" in the parameter of hasColumn that specifies the validators for that column.
parent 05cf2cd1
...@@ -149,6 +149,14 @@ final class Doctrine { ...@@ -149,6 +149,14 @@ final class Doctrine {
* accessor invoking attribute * accessor invoking attribute
*/ */
const ATTR_ACCESSORS = 18; const ATTR_ACCESSORS = 18;
/**
* automatic length validations attribute
*/
const ATTR_AUTO_LENGTH_VLD = 19;
/**
* automatic type validations attribute
*/
const ATTR_AUTO_TYPE_VLD = 20;
/** /**
......
...@@ -115,6 +115,8 @@ abstract class Doctrine_Configurable { ...@@ -115,6 +115,8 @@ abstract class Doctrine_Configurable {
break; break;
case Doctrine::ATTR_VLD: case Doctrine::ATTR_VLD:
case Doctrine::ATTR_AUTO_LENGTH_VLD:
case Doctrine::ATTR_AUTO_TYPE_VLD:
case Doctrine::ATTR_QUERY_LIMIT: case Doctrine::ATTR_QUERY_LIMIT:
break; break;
...@@ -188,7 +190,7 @@ abstract class Doctrine_Configurable { ...@@ -188,7 +190,7 @@ abstract class Doctrine_Configurable {
public function getAttribute($attribute) { public function getAttribute($attribute) {
$attribute = (int) $attribute; $attribute = (int) $attribute;
if($attribute < 1 || $attribute > 18) if($attribute < 1 || $attribute > 20)
throw new InvalidKeyException(); throw new InvalidKeyException();
if( ! isset($this->attributes[$attribute])) { if( ! isset($this->attributes[$attribute])) {
......
...@@ -90,6 +90,8 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -90,6 +90,8 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
Doctrine::ATTR_LISTENER => new Doctrine_EventListener(), Doctrine::ATTR_LISTENER => new Doctrine_EventListener(),
Doctrine::ATTR_LOCKMODE => 1, Doctrine::ATTR_LOCKMODE => 1,
Doctrine::ATTR_VLD => false, Doctrine::ATTR_VLD => false,
Doctrine::ATTR_AUTO_LENGTH_VLD => true,
Doctrine::ATTR_AUTO_TYPE_VLD => true,
Doctrine::ATTR_CREATE_TABLES => true, Doctrine::ATTR_CREATE_TABLES => true,
Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS
); );
......
...@@ -105,15 +105,12 @@ class Doctrine_Validator { ...@@ -105,15 +105,12 @@ class Doctrine_Validator {
} }
} }
if($column[0] == "array" || $column[0] == "object") if($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) {
$length = strlen(serialize($value)); if(!$this->validateLength($column, $key, $value)) {
else
$length = strlen($value);
if($length > $column[1]) {
$errorStack->add($key, 'length'); $errorStack->add($key, 'length');
continue; continue;
} }
}
if( ! is_array($column[2])) if( ! is_array($column[2]))
$e = explode("|",$column[2]); $e = explode("|",$column[2]);
...@@ -138,6 +135,24 @@ class Doctrine_Validator { ...@@ -138,6 +135,24 @@ class Doctrine_Validator {
$name == 'default') $name == 'default')
continue; continue;
if(strtolower($name) == 'length') {
if(!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) {
if(!$this->validateLength($column, $key, $value)) {
$errorStack->add($key, 'length');
}
}
continue;
}
if(strtolower($name) == 'type') {
if(!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) {
if( ! self::isValidType($value, $column[0])) {
$errorStack->add($key, 'type');
}
}
continue;
}
$validator = self::getValidator($name); $validator = self::getValidator($name);
if( ! $validator->validate($record, $key, $value, $args)) { if( ! $validator->validate($record, $key, $value, $args)) {
...@@ -147,15 +162,33 @@ class Doctrine_Validator { ...@@ -147,15 +162,33 @@ class Doctrine_Validator {
//$err[$key] = 'not valid'; //$err[$key] = 'not valid';
// errors found quit validation looping for this column // errors found quit validation looping for this column
break; //break;
} }
} }
if($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) {
if( ! self::isValidType($value, $column[0])) { if( ! self::isValidType($value, $column[0])) {
$errorStack->add($key, 'type'); $errorStack->add($key, 'type');
continue; continue;
} }
} }
} }
}
/**
* Enter description here...
*
*/
private function validateLength($column, $key, $value) {
if($column[0] == "array" || $column[0] == "object")
$length = strlen(serialize($value));
else
$length = strlen($value);
if($length > $column[1]) {
return false;
}
return true;
}
/** /**
* whether or not this validator has errors * whether or not this validator has errors
* *
......
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