1
0
mirror of synced 2025-02-22 07:03:13 +03:00

Improved validation API

require_once bug fix
This commit is contained in:
doctrine 2006-05-24 22:33:48 +00:00
parent ced9625b64
commit 86b319e618
34 changed files with 124 additions and 29 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
require_once("Access.class.php");
/** /**
* Doctrine_Collection * Doctrine_Collection
* Collection of Doctrine_Record objects. * Collection of Doctrine_Record objects.

View File

@ -1,5 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Collection.class.php"); require_once("Batch.class.php");
/** /**
* a collection of Doctrine_Record objects with lazy load strategy * a collection of Doctrine_Record objects with lazy load strategy
* (batch load strategy with batch size 1) * (batch load strategy with batch size 1)

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/** /**
* thrown when user tries to find a Doctrine_Record for given primary key and that object is not found * thrown when user tries to find a Doctrine_Record for given primary key and that object is not found
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/** /**
* thrown when user tries to get a foreign key object but the mapping is not done right * thrown when user tries to get a foreign key object but the mapping is not done right
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/** /**
* thrown when user defined Doctrine_Table is badly named * thrown when user defined Doctrine_Table is badly named
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/** /**
* thrown when Doctrine_Record is loaded and there is no primary key field * thrown when Doctrine_Record is loaded and there is no primary key field
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/** /**
* thrown when Doctrine_Record is refreshed and the refreshed primary key doens't match the old one * thrown when Doctrine_Record is refreshed and the refreshed primary key doens't match the old one
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/** /**
* thrown when user tries to get the current * thrown when user tries to get the current
* session and there are no open sessions * session and there are no open sessions

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/** /**
* thrown when user tries to initialize a new instance of Doctrine_Table, * thrown when user tries to initialize a new instance of Doctrine_Table,
* while there already exists an instance of that table * while there already exists an instance of that table

View File

@ -1,4 +1,6 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
class Doctrine_Validator_Exception extends Doctrine_Exception { class Doctrine_Validator_Exception extends Doctrine_Exception {
private $validator; private $validator;
public function __construct(Doctrine_Validator $validator) { public function __construct(Doctrine_Validator $validator) {

View File

@ -1,4 +1,5 @@
<?php <?php
require_once("Access.class.php");
/** /**
* Doctrine_Query * Doctrine_Query
* *

View File

@ -533,6 +533,31 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->references[$name] = $value; $this->references[$name] = $value;
} }
} }
/**
* __isset
*
* @param string $name
* @return boolean
*/
public function __isset($name) {
if(isset($this->data[$name]))
return true;
if(isset($this->references[$name]))
return true;
return false;
}
/**
* @param string $name
* @return void
*/
public function __unset($name) {
if(isset($this->data[$name]))
$this->data[$name] = array();
// todo: what to do with references ?
}
/** /**
* applies the changes made to this object into database * applies the changes made to this object into database
* this method is smart enough to know if any changes are made * this method is smart enough to know if any changes are made

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/** /**
* standard session, the parent of pgsql, mysql and sqlite * standard session, the parent of pgsql, mysql and sqlite
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/** /**
* firebird driver * firebird driver
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/** /**
* informix database driver * informix database driver
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/** /**
* mssql driver * mssql driver
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once("Common.class.php");
/** /**
* mysql driver * mysql driver
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/** /**
* oracle driver * oracle driver
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once("Common.class.php");
/** /**
* pgsql driver * pgsql driver
*/ */

View File

@ -1,4 +1,5 @@
<?php <?php
require_once("Common.class.php");
/** /**
* sqlite driver * sqlite driver
*/ */

View File

@ -28,6 +28,29 @@ class Doctrine_Validator {
* constant for unique validation error * constant for unique validation error
*/ */
const ERR_UNIQUE = 3; const ERR_UNIQUE = 3;
/**
* constant for blank validation error
*/
const ERR_BLANK = 4;
/**
* constant for date validation error
*/
const ERR_DATE = 5;
/**
* constant for null validation error
*/
const ERR_NULL = 6;
/**
* constant for enum validation error
*/
const ERR_ENUM = 7;
/**
* constant for range validation error
*/
const ERR_RANGE = 8;
/** /**
* @var array $stack error stack * @var array $stack error stack
@ -84,20 +107,35 @@ class Doctrine_Validator {
$e = explode("|",$column[2]); $e = explode("|",$column[2]);
foreach($e as $k => $arg) { foreach($e as $k => $arg) {
if(empty($arg) || $arg == "primary") if(empty($arg) || $arg == "primary" || $arg == "protected" || $arg == "autoincrement")
continue; continue;
$validator = self::getValidator($arg); $args = explode(":",$arg);
if( ! $validator->validate($record,$key,$value)) { if( ! isset($args[1]))
switch(strtolower($arg)): $args[1] = '';
$validator = self::getValidator($args[0]);
if( ! $validator->validate($record, $key, $value, $args[1])) {
switch(strtolower($args[0])):
case "unique": case "unique":
$err[$key] = Doctrine_Validator::ERR_UNIQUE; $err[$key] = Doctrine_Validator::ERR_UNIQUE;
break; break;
case "notnull":
$err[$key] = Doctrine_Validator::ERR_NULL;
break;
case "notblank":
$err[$key] = Doctrine_Validator::ERR_BLANK;
break;
case "enum":
$err[$key] = Doctrine_Validator::ERR_VALID;
break;
default: default:
$err[$key] = Doctrine_Validator::ERR_VALID; $err[$key] = Doctrine_Validator::ERR_VALID;
break; break;
endswitch; endswitch;
} }
// errors found quit validation looping for this column
if(isset($err[$key])) if(isset($err[$key]))
break; break;
} }

View File

@ -254,9 +254,10 @@ class Doctrine_Validator_Country {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
return isset(self::$countries[$value]); return isset(self::$countries[$value]);
} }

View File

@ -4,9 +4,10 @@ class Doctrine_Validator_Date {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
return checkdate($value); return checkdate($value);
} }
} }

View File

@ -4,9 +4,10 @@ class Doctrine_Validator_Email {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
$parts = explode("@", $value); $parts = explode("@", $value);
if(count($parts) != 2) if(count($parts) != 2)

View File

@ -4,9 +4,10 @@ class Doctrine_Validator_HtmlColor {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
if( ! preg_match("/^#{0,1}[0-9]{6}$/",$color)) { if( ! preg_match("/^#{0,1}[0-9]{6}$/",$color)) {
return false; return false;
} }

View File

@ -4,9 +4,10 @@ class Doctrine_Validator_Ip {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
$e = explode(".",$request); $e = explode(".",$request);
if(count($e) != 4) return false; if(count($e) != 4) return false;

View File

@ -4,9 +4,10 @@ class Doctrine_Validator_NoSpace {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
if(preg_match("/[\s\r\t\n]/", $value)) if(preg_match("/[\s\r\t\n]/", $value))
return false; return false;

View File

@ -4,15 +4,18 @@ class Doctrine_Validator_Notblank {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
$string = str_replace("\n","",$value); $string = str_replace("\n","",$value);
$string = str_replace("\r","",$string); $string = str_replace("\r","",$string);
$string = str_replace("\t","",$string); $string = str_replace("\t","",$string);
$string = str_replace("\s","",$string); $string = str_replace("\s","",$string);
$string = str_replace(" ","",$string); $string = str_replace(" ","",$string);
if($string == "") return true; if($string == "") return false;
return true;
} }
} }
?> ?>

View File

@ -16,9 +16,10 @@ class Doctrine_Validator_Range {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
if($var < $this->min) if($var < $this->min)
return false; return false;

View File

@ -4,9 +4,10 @@ class Doctrine_Validator_Regexp {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
return $value; return $value;
} }
} }

View File

@ -4,9 +4,10 @@ class Doctrine_Validator_Unique {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
$table = $record->getTable(); $table = $record->getTable();
$sql = "SELECT id FROM ".$table->getTableName()." WHERE ".$key." = ?"; $sql = "SELECT id FROM ".$table->getTableName()." WHERE ".$key." = ?";
$stmt = $table->getSession()->getDBH()->prepare($sql); $stmt = $table->getSession()->getDBH()->prepare($sql);

View File

@ -62,9 +62,10 @@ class ValidatorUSState {
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param string $args
* @return boolean * @return boolean
*/ */
public function validate(Doctrine_Record $record, $key, $value) { public function validate(Doctrine_Record $record, $key, $value, $args) {
return isset(self::$states[$value]); return isset(self::$states[$value]);
} }
} }

View File

@ -39,13 +39,13 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
$validator = new Doctrine_Validator_Email(); $validator = new Doctrine_Validator_Email();
$email = $this->session->create("Email"); $email = $this->session->create("Email");
$this->assertFalse($validator->validate($email,"address","example@example")); $this->assertFalse($validator->validate($email,"address","example@example",null));
$this->assertFalse($validator->validate($email,"address","example@@example")); $this->assertFalse($validator->validate($email,"address","example@@example",null));
$this->assertFalse($validator->validate($email,"address","example@example.")); $this->assertFalse($validator->validate($email,"address","example@example.",null));
$this->assertFalse($validator->validate($email,"address","example@e..")); $this->assertFalse($validator->validate($email,"address","example@e..",null));
$this->assertFalse($validator->validate($email,"address","example@e..")); $this->assertFalse($validator->validate($email,"address","example@e..",null));
$this->assertTrue($validator->validate($email,"address","example@e.e.e.e.e")); $this->assertTrue($validator->validate($email,"address","example@e.e.e.e.e",null));
} }
public function testSave() { public function testSave() {