Support for passing an array as constraint/validator argument
This commit is contained in:
parent
7bb07a5be1
commit
276af65256
@ -34,8 +34,17 @@ class Doctrine_DataDict {
|
||||
foreach($columns as $name => $args) {
|
||||
if( ! is_array($args[2]))
|
||||
$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 {
|
||||
* constant for range validation error
|
||||
*/
|
||||
const ERR_RANGE = 8;
|
||||
|
||||
/**
|
||||
* constant for regexp validation error
|
||||
*/
|
||||
const ERR_REGEXP = 9;
|
||||
|
||||
|
||||
|
||||
@ -119,8 +122,8 @@ class Doctrine_Validator {
|
||||
* @return void
|
||||
*/
|
||||
public function validateRecord(Doctrine_Record $record) {
|
||||
$columns = $record->getTable()->getColumns();
|
||||
$name = $record->getTable()->getComponentName();
|
||||
$columns = $record->getTable()->getColumns();
|
||||
$component = $record->getTable()->getComponentName();
|
||||
|
||||
switch($record->getState()):
|
||||
case Doctrine_Record::STATE_TDIRTY:
|
||||
@ -165,21 +168,23 @@ class Doctrine_Validator {
|
||||
|
||||
|
||||
foreach($e as $k => $arg) {
|
||||
if(empty($arg) || $arg == "primary" || $arg == "protected" || $arg == "autoincrement")
|
||||
if(is_string($k)) {
|
||||
$name = $k;
|
||||
$args = $arg;
|
||||
} else {
|
||||
$args = explode(":",$arg);
|
||||
$name = array_shift($args);
|
||||
if( ! isset($args[0]))
|
||||
$args[0] = '';
|
||||
}
|
||||
|
||||
if(empty($name) || $name == "primary" || $name == "protected" || $name == "autoincrement")
|
||||
continue;
|
||||
|
||||
if( ! is_integer($k)) {
|
||||
$args = $arg;
|
||||
} else
|
||||
$args = explode(":",$arg);
|
||||
|
||||
if( ! isset($args[1]))
|
||||
$args[1] = '';
|
||||
$validator = self::getValidator($name);
|
||||
if( ! $validator->validate($record, $key, $value, $args)) {
|
||||
|
||||
$validator = self::getValidator($args[0]);
|
||||
if( ! $validator->validate($record, $key, $value, $args[1])) {
|
||||
|
||||
$constant = 'Doctrine_Validator::ERR_'.strtoupper($args[0]);
|
||||
$constant = 'Doctrine_Validator::ERR_'.strtoupper($name);
|
||||
|
||||
if(defined($constant))
|
||||
$err[$key] = constant($constant);
|
||||
@ -197,7 +202,7 @@ class Doctrine_Validator {
|
||||
}
|
||||
|
||||
if( ! empty($err)) {
|
||||
$this->stack[$name][] = $err;
|
||||
$this->stack[$component][] = $err;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -8,11 +8,10 @@ class Doctrine_Validator_Range {
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate(Doctrine_Record $record, $key, $value, $args) {
|
||||
$e = explode("-",$args);
|
||||
if($value < $e[0])
|
||||
if(isset($args[0]) && $value < $args[0])
|
||||
return false;
|
||||
|
||||
if(isset($e[1]) && $value > $e[1])
|
||||
|
||||
if(isset($args[1]) && $value > $args[1])
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
@ -8,9 +8,17 @@ class Doctrine_Validator_Regexp {
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate(Doctrine_Record $record, $key, $value, $args) {
|
||||
if(preg_match("/$args/", $value))
|
||||
if(is_array($args)) {
|
||||
foreach($args as $regexp) {
|
||||
if( ! preg_match("/$args/", $value))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
} else {
|
||||
if(preg_match("/$args/", $value))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,9 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
|
||||
public function testValidate2() {
|
||||
$test = new ValidatorTest();
|
||||
$test->mymixed = "message";
|
||||
|
||||
$test->myrange = 1;
|
||||
$test->myregexp = '123a';
|
||||
|
||||
$validator = new Doctrine_Validator();
|
||||
$validator->validateRecord($test);
|
||||
|
||||
@ -85,6 +87,8 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
|
||||
|
||||
$this->assertEqual($stack['mystring'], Doctrine_Validator::ERR_NOTNULL);
|
||||
$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';
|
||||
|
||||
|
||||
|
@ -397,6 +397,9 @@ class ValidatorTest extends Doctrine_Record {
|
||||
$this->hasColumn("myarray", "array", 1000);
|
||||
$this->hasColumn("myobject", "object", 1000);
|
||||
$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("myemail2", "string", 100, "email|notblank");
|
||||
}
|
||||
|
@ -62,8 +62,6 @@ $test->addTestCase(new Doctrine_Filter_TestCase());
|
||||
|
||||
$test->addTestCase(new Doctrine_ValueHolder_TestCase());
|
||||
|
||||
$test->addTestCase(new Doctrine_ValidatorTestCase());
|
||||
|
||||
$test->addTestCase(new Doctrine_QueryTestCase());
|
||||
|
||||
$test->addTestCase(new Doctrine_RawSql_TestCase());
|
||||
@ -76,6 +74,7 @@ $test->addTestCase(new Doctrine_ImportTestCase());
|
||||
|
||||
$test->addTestCase(new Doctrine_CollectionTestCase());
|
||||
|
||||
$test->addTestCase(new Doctrine_ValidatorTestCase());
|
||||
//$test->addTestCase(new Doctrine_Cache_FileTestCase());
|
||||
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user