Commit 276af652 authored by zYne's avatar zYne

Support for passing an array as constraint/validator argument

parent 7bb07a5b
...@@ -35,7 +35,16 @@ class Doctrine_DataDict { ...@@ -35,7 +35,16 @@ class Doctrine_DataDict {
if( ! is_array($args[2])) if( ! is_array($args[2]))
$args[2] = array(); $args[2] = array();
$r[] = $name." ".$this->getADOType($args[0],$args[1])." ".implode(' ',$args[2]); $constraints = array();
foreach($args[2] as $k => $v) {
if(is_string($k))
$constraints[] = $k;
else
$constraints[] = $v;
}
$r[] = $name." ".$this->getADOType($args[0],$args[1])." ".implode(' ', $constraints);
} }
......
...@@ -67,7 +67,10 @@ class Doctrine_Validator { ...@@ -67,7 +67,10 @@ class Doctrine_Validator {
* constant for range validation error * constant for range validation error
*/ */
const ERR_RANGE = 8; const ERR_RANGE = 8;
/**
* constant for regexp validation error
*/
const ERR_REGEXP = 9;
...@@ -120,7 +123,7 @@ class Doctrine_Validator { ...@@ -120,7 +123,7 @@ class Doctrine_Validator {
*/ */
public function validateRecord(Doctrine_Record $record) { public function validateRecord(Doctrine_Record $record) {
$columns = $record->getTable()->getColumns(); $columns = $record->getTable()->getColumns();
$name = $record->getTable()->getComponentName(); $component = $record->getTable()->getComponentName();
switch($record->getState()): switch($record->getState()):
case Doctrine_Record::STATE_TDIRTY: case Doctrine_Record::STATE_TDIRTY:
...@@ -165,21 +168,23 @@ class Doctrine_Validator { ...@@ -165,21 +168,23 @@ class Doctrine_Validator {
foreach($e as $k => $arg) { foreach($e as $k => $arg) {
if(empty($arg) || $arg == "primary" || $arg == "protected" || $arg == "autoincrement") if(is_string($k)) {
continue; $name = $k;
if( ! is_integer($k)) {
$args = $arg; $args = $arg;
} else } else {
$args = explode(":",$arg); $args = explode(":",$arg);
$name = array_shift($args);
if( ! isset($args[0]))
$args[0] = '';
}
if( ! isset($args[1])) if(empty($name) || $name == "primary" || $name == "protected" || $name == "autoincrement")
$args[1] = ''; continue;
$validator = self::getValidator($args[0]); $validator = self::getValidator($name);
if( ! $validator->validate($record, $key, $value, $args[1])) { if( ! $validator->validate($record, $key, $value, $args)) {
$constant = 'Doctrine_Validator::ERR_'.strtoupper($args[0]); $constant = 'Doctrine_Validator::ERR_'.strtoupper($name);
if(defined($constant)) if(defined($constant))
$err[$key] = constant($constant); $err[$key] = constant($constant);
...@@ -197,7 +202,7 @@ class Doctrine_Validator { ...@@ -197,7 +202,7 @@ class Doctrine_Validator {
} }
if( ! empty($err)) { if( ! empty($err)) {
$this->stack[$name][] = $err; $this->stack[$component][] = $err;
return false; return false;
} }
......
...@@ -8,11 +8,10 @@ class Doctrine_Validator_Range { ...@@ -8,11 +8,10 @@ class Doctrine_Validator_Range {
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value, $args) { public function validate(Doctrine_Record $record, $key, $value, $args) {
$e = explode("-",$args); if(isset($args[0]) && $value < $args[0])
if($value < $e[0])
return false; return false;
if(isset($e[1]) && $value > $e[1]) if(isset($args[1]) && $value > $args[1])
return false; return false;
return true; return true;
......
...@@ -8,8 +8,16 @@ class Doctrine_Validator_Regexp { ...@@ -8,8 +8,16 @@ class Doctrine_Validator_Regexp {
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value, $args) { public function validate(Doctrine_Record $record, $key, $value, $args) {
if(is_array($args)) {
foreach($args as $regexp) {
if( ! preg_match("/$args/", $value))
return false;
}
return true;
} else {
if(preg_match("/$args/", $value)) if(preg_match("/$args/", $value))
return true; return true;
}
return false; return false;
} }
......
...@@ -72,6 +72,8 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { ...@@ -72,6 +72,8 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
public function testValidate2() { public function testValidate2() {
$test = new ValidatorTest(); $test = new ValidatorTest();
$test->mymixed = "message"; $test->mymixed = "message";
$test->myrange = 1;
$test->myregexp = '123a';
$validator = new Doctrine_Validator(); $validator = new Doctrine_Validator();
$validator->validateRecord($test); $validator->validateRecord($test);
...@@ -85,6 +87,8 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { ...@@ -85,6 +87,8 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($stack['mystring'], Doctrine_Validator::ERR_NOTNULL); $this->assertEqual($stack['mystring'], Doctrine_Validator::ERR_NOTNULL);
$this->assertEqual($stack['myemail2'], Doctrine_Validator::ERR_NOTBLANK); $this->assertEqual($stack['myemail2'], Doctrine_Validator::ERR_NOTBLANK);
$this->assertEqual($stack['myrange'], Doctrine_Validator::ERR_RANGE);
$this->assertEqual($stack['myregexp'], Doctrine_Validator::ERR_REGEXP);
$test->mystring = 'str'; $test->mystring = 'str';
......
...@@ -397,6 +397,9 @@ class ValidatorTest extends Doctrine_Record { ...@@ -397,6 +397,9 @@ class ValidatorTest extends Doctrine_Record {
$this->hasColumn("myarray", "array", 1000); $this->hasColumn("myarray", "array", 1000);
$this->hasColumn("myobject", "object", 1000); $this->hasColumn("myobject", "object", 1000);
$this->hasColumn("myinteger", "integer", 11); $this->hasColumn("myinteger", "integer", 11);
$this->hasColumn("myrange", "integer", 11, array('range' => array(4,123)));
$this->hasColumn("myregexp", "string", 5, array('regexp' => '^[0-9]+$'));
$this->hasColumn("myemail", "string", 100, "email"); $this->hasColumn("myemail", "string", 100, "email");
$this->hasColumn("myemail2", "string", 100, "email|notblank"); $this->hasColumn("myemail2", "string", 100, "email|notblank");
} }
......
...@@ -62,8 +62,6 @@ $test->addTestCase(new Doctrine_Filter_TestCase()); ...@@ -62,8 +62,6 @@ $test->addTestCase(new Doctrine_Filter_TestCase());
$test->addTestCase(new Doctrine_ValueHolder_TestCase()); $test->addTestCase(new Doctrine_ValueHolder_TestCase());
$test->addTestCase(new Doctrine_ValidatorTestCase());
$test->addTestCase(new Doctrine_QueryTestCase()); $test->addTestCase(new Doctrine_QueryTestCase());
$test->addTestCase(new Doctrine_RawSql_TestCase()); $test->addTestCase(new Doctrine_RawSql_TestCase());
...@@ -76,6 +74,7 @@ $test->addTestCase(new Doctrine_ImportTestCase()); ...@@ -76,6 +74,7 @@ $test->addTestCase(new Doctrine_ImportTestCase());
$test->addTestCase(new Doctrine_CollectionTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase());
$test->addTestCase(new Doctrine_ValidatorTestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
......
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