Skip to content

Commit 66fb681

Browse files
committed
Validation expanded
1 parent 851dcfe commit 66fb681

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

dbObject.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ private function processArrays (&$data) {
462462
* @param array $data
463463
*/
464464
private function validate ($data) {
465-
$this->errors = Array ();
466465
foreach ($this->dbFields as $key => $desc) {
467466
$type = null;
468467
$required = false;
@@ -480,7 +479,7 @@ private function validate ($data) {
480479
$required = true;
481480

482481
if ($required && strlen ($value) == 0) {
483-
$this->errors[] = Array ($key => "is required");
482+
$this->errors[] = Array ($this->dbTable . "." . $key => "is required");
484483
continue;
485484
}
486485
if ($value == null)
@@ -507,14 +506,15 @@ private function validate ($data) {
507506
continue;
508507

509508
if (!preg_match ($regexp, $value)) {
510-
$this->errors[] = Array ($key => "$type validation failed");
509+
$this->errors[] = Array ($this->dbTable . "." . $key => "$type validation failed");
511510
continue;
512511
}
513512
}
514513
return !count ($this->errors) > 0;
515514
}
516515

517516
private function prepareData () {
517+
$this->errors = Array ();
518518
$sqlData = Array();
519519
if (count ($this->data) == 0)
520520
return Array();
@@ -523,8 +523,13 @@ private function prepareData () {
523523
$this->preLoad ($data);
524524

525525
foreach ($this->data as $key => &$value) {
526-
if ($value instanceof dbObject && $value->isNew == true)
527-
$value = $value->save();
526+
if ($value instanceof dbObject && $value->isNew == true) {
527+
$id = $value->save();
528+
if ($id)
529+
$value = $id;
530+
else
531+
$this->errors = array_merge ($this->errors, $value->errors);
532+
}
528533

529534
if (!in_array ($key, array_keys ($this->dbFields)))
530535
continue;

tests/dbObjectTests.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?
2+
error_reporting (E_STRICT);
23
require_once ("../MysqliDb.php");
34
require_once ("../dbObject.php");
45
require_once ("models/product.php");
@@ -98,6 +99,7 @@ function createTable ($name, $data) {
9899
$obj = new $name ($userData);
99100
$id = $obj->save();
100101
if ($obj->errors) {
102+
echo "errors:";
101103
print_r ($obj->errors);
102104
exit;
103105
}
@@ -196,6 +198,7 @@ function createTable ($name, $data) {
196198
$obj->userId = $client;
197199
$obj->save();
198200
if ($client->errors) {
201+
echo "errors:";
199202
print_r ($client->errors);
200203
exit;
201204
}
@@ -208,16 +211,21 @@ function createTable ($name, $data) {
208211
exit;
209212
}
210213

211-
$obj = new user;
212-
$obj->active='test';
213-
$obj->customerId = 'test';
214-
$obj->expires = 'test;';
215-
$obj->firstName = 'test';
214+
$u= new user;
215+
$u->active='test';
216+
$u->customerId = 'test';
217+
$u->expires = 'test;';
218+
$u->firstName = 'test';
219+
220+
$obj = new product;
221+
$obj->userId = $u;
222+
$obj->save();
216223
if ($obj->save()) {
217224
echo "validation 1 failed\n";
218225
exit;
219226
}
220-
if (count ($obj->errors) != 4) {
227+
if (count ($obj->errors) != 7) {
228+
print_r ($obj->errors);
221229
echo "validation 2 failed\n";
222230
exit;
223231
}

0 commit comments

Comments
 (0)