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

Validation expanded

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