Commit 86b319e6 authored by doctrine's avatar doctrine

Improved validation API

require_once bug fix
parent ced9625b
<?php
require_once("Access.class.php");
/**
* Doctrine_Collection
* Collection of Doctrine_Record objects.
......
<?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
* (batch load strategy with batch size 1)
......
<?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
*/
......
<?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
*/
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/**
* thrown when user defined Doctrine_Table is badly named
*/
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/**
* thrown when Doctrine_Record is loaded and there is no primary key field
*/
......
<?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
*/
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/**
* thrown when user tries to get the current
* session and there are no open sessions
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
/**
* thrown when user tries to initialize a new instance of Doctrine_Table,
* while there already exists an instance of that table
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Exception.class.php");
class Doctrine_Validator_Exception extends Doctrine_Exception {
private $validator;
public function __construct(Doctrine_Validator $validator) {
......
<?php
require_once("Access.class.php");
/**
* Doctrine_Query
*
......
......@@ -533,6 +533,31 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$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
* this method is smart enough to know if any changes are made
......
......@@ -94,7 +94,7 @@ class Sensei extends Doctrine_Access {
public function __construct() {
if(headers_sent())
if(headers_sent())
throw new Sensei_Exception("Headers already sent. Couldn't initialize session.");
$this->session = Doctrine_Manager::getInstance()->getCurrentSession();
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/**
* standard session, the parent of pgsql, mysql and sqlite
*/
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/**
* firebird driver
*/
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/**
* informix database driver
*/
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/**
* mssql driver
*/
......
<?php
require_once("Common.class.php");
/**
* mysql driver
*/
......
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Session.class.php");
/**
* oracle driver
*/
......
<?php
require_once("Common.class.php");
/**
* pgsql driver
*/
......
<?php
require_once("Common.class.php");
/**
* sqlite driver
*/
......
......@@ -15,19 +15,42 @@ class Doctrine_Validator {
/**
* constant for length validation error
*/
const ERR_LENGTH = 0;
const ERR_LENGTH = 0;
/**
* constant for type validation error
*/
const ERR_TYPE = 1;
const ERR_TYPE = 1;
/**
* constant for general validation error
*/
const ERR_VALID = 2;
const ERR_VALID = 2;
/**
* 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
......@@ -74,7 +97,7 @@ class Doctrine_Validator {
if(strlen($value) > $column[1]) {
$err[$key] = Doctrine_Validator::ERR_LENGTH;
continue;
}
}
if(self::gettype($value) !== $column[0]) {
$err[$key] = Doctrine_Validator::ERR_TYPE;
......@@ -84,20 +107,35 @@ class Doctrine_Validator {
$e = explode("|",$column[2]);
foreach($e as $k => $arg) {
if(empty($arg) || $arg == "primary")
if(empty($arg) || $arg == "primary" || $arg == "protected" || $arg == "autoincrement")
continue;
$validator = self::getValidator($arg);
if( ! $validator->validate($record,$key,$value)) {
switch(strtolower($arg)):
$args = explode(":",$arg);
if( ! isset($args[1]))
$args[1] = '';
$validator = self::getValidator($args[0]);
if( ! $validator->validate($record, $key, $value, $args[1])) {
switch(strtolower($args[0])):
case "unique":
$err[$key] = Doctrine_Validator::ERR_UNIQUE;
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:
$err[$key] = Doctrine_Validator::ERR_VALID;
break;
endswitch;
}
// errors found quit validation looping for this column
if(isset($err[$key]))
break;
}
......
......@@ -254,9 +254,10 @@ class Doctrine_Validator_Country {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
public function validate(Doctrine_Record $record, $key, $value, $args) {
return isset(self::$countries[$value]);
}
......
......@@ -4,9 +4,10 @@ class Doctrine_Validator_Date {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
public function validate(Doctrine_Record $record, $key, $value, $args) {
return checkdate($value);
}
}
......
......@@ -4,9 +4,10 @@ class Doctrine_Validator_Email {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
public function validate(Doctrine_Record $record, $key, $value, $args) {
$parts = explode("@", $value);
if(count($parts) != 2)
......
......@@ -4,9 +4,10 @@ class Doctrine_Validator_HtmlColor {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @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)) {
return false;
}
......
......@@ -4,9 +4,10 @@ class Doctrine_Validator_Ip {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
public function validate(Doctrine_Record $record, $key, $value, $args) {
$e = explode(".",$request);
if(count($e) != 4) return false;
......
......@@ -4,9 +4,10 @@ class Doctrine_Validator_NoSpace {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @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))
return false;
......
......@@ -4,15 +4,18 @@ class Doctrine_Validator_Notblank {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @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("\r","",$string);
$string = str_replace("\t","",$string);
$string = str_replace("\s","",$string);
$string = str_replace(" ","",$string);
if($string == "") return true;
if($string == "") return false;
return true;
}
}
?>
......@@ -16,9 +16,10 @@ class Doctrine_Validator_Range {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
public function validate(Doctrine_Record $record, $key, $value, $args) {
if($var < $this->min)
return false;
......
......@@ -4,9 +4,10 @@ class Doctrine_Validator_Regexp {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
public function validate(Doctrine_Record $record, $key, $value, $args) {
return $value;
}
}
......
......@@ -4,9 +4,10 @@ class Doctrine_Validator_Unique {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
public function validate(Doctrine_Record $record, $key, $value, $args) {
$table = $record->getTable();
$sql = "SELECT id FROM ".$table->getTableName()." WHERE ".$key." = ?";
$stmt = $table->getSession()->getDBH()->prepare($sql);
......
......@@ -62,9 +62,10 @@ class ValidatorUSState {
* @param Doctrine_Record $record
* @param string $key
* @param mixed $value
* @param string $args
* @return boolean
*/
public function validate(Doctrine_Record $record, $key, $value) {
public function validate(Doctrine_Record $record, $key, $value, $args) {
return isset(self::$states[$value]);
}
}
......
......@@ -39,13 +39,13 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
$validator = new Doctrine_Validator_Email();
$email = $this->session->create("Email");
$this->assertFalse($validator->validate($email,"address","example@example"));
$this->assertFalse($validator->validate($email,"address","example@@example"));
$this->assertFalse($validator->validate($email,"address","example@example."));
$this->assertFalse($validator->validate($email,"address","example@e.."));
$this->assertFalse($validator->validate($email,"address","example@example",null));
$this->assertFalse($validator->validate($email,"address","example@@example",null));
$this->assertFalse($validator->validate($email,"address","example@example.",null));
$this->assertFalse($validator->validate($email,"address","example@e..",null));
$this->assertFalse($validator->validate($email,"address","example@e.."));
$this->assertTrue($validator->validate($email,"address","example@e.e.e.e.e"));
$this->assertFalse($validator->validate($email,"address","example@e..",null));
$this->assertTrue($validator->validate($email,"address","example@e.e.e.e.e",null));
}
public function testSave() {
......
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