Commit 66fb6818 authored by Alexander Butenko's avatar Alexander Butenko

Validation expanded

parent 851dcfe2
......@@ -462,7 +462,6 @@ class dbObject {
* @param array $data
*/
private function validate ($data) {
$this->errors = Array ();
foreach ($this->dbFields as $key => $desc) {
$type = null;
$required = false;
......@@ -480,7 +479,7 @@ class dbObject {
$required = true;
if ($required && strlen ($value) == 0) {
$this->errors[] = Array ($key => "is required");
$this->errors[] = Array ($this->dbTable . "." . $key => "is required");
continue;
}
if ($value == null)
......@@ -507,7 +506,7 @@ class dbObject {
continue;
if (!preg_match ($regexp, $value)) {
$this->errors[] = Array ($key => "$type validation failed");
$this->errors[] = Array ($this->dbTable . "." . $key => "$type validation failed");
continue;
}
}
......@@ -515,6 +514,7 @@ class dbObject {
}
private function prepareData () {
$this->errors = Array ();
$sqlData = Array();
if (count ($this->data) == 0)
return Array();
......@@ -523,8 +523,13 @@ class dbObject {
$this->preLoad ($data);
foreach ($this->data as $key => &$value) {
if ($value instanceof dbObject && $value->isNew == true)
$value = $value->save();
if ($value instanceof dbObject && $value->isNew == true) {
$id = $value->save();
if ($id)
$value = $id;
else
$this->errors = array_merge ($this->errors, $value->errors);
}
if (!in_array ($key, array_keys ($this->dbFields)))
continue;
......
<?
error_reporting (E_STRICT);
require_once ("../MysqliDb.php");
require_once ("../dbObject.php");
require_once ("models/product.php");
......@@ -98,6 +99,7 @@ foreach ($data as $name => $datas) {
$obj = new $name ($userData);
$id = $obj->save();
if ($obj->errors) {
echo "errors:";
print_r ($obj->errors);
exit;
}
......@@ -196,6 +198,7 @@ $obj->save();
$obj->userId = $client;
$obj->save();
if ($client->errors) {
echo "errors:";
print_r ($client->errors);
exit;
}
......@@ -208,16 +211,21 @@ if ($obj->with('userId')->toJson() != $expected) {
exit;
}
$obj = new user;
$obj->active='test';
$obj->customerId = 'test';
$obj->expires = 'test;';
$obj->firstName = 'test';
$u= new user;
$u->active='test';
$u->customerId = 'test';
$u->expires = 'test;';
$u->firstName = 'test';
$obj = new product;
$obj->userId = $u;
$obj->save();
if ($obj->save()) {
echo "validation 1 failed\n";
exit;
}
if (count ($obj->errors) != 4) {
if (count ($obj->errors) != 7) {
print_r ($obj->errors);
echo "validation 2 failed\n";
exit;
}
......
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