Skip to content

Commit 415b805

Browse files
committed
Merge branch 'develop' of github.com:bcit-ci/CodeIgniter4 into develop
2 parents bea1dd5 + 4c57656 commit 415b805

3 files changed

Lines changed: 235 additions & 61 deletions

File tree

system/Model.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ public function update($id = null, $data = null)
727727
{
728728
if ($this->validate($data) === false)
729729
{
730+
dd($this->errors());
730731
return false;
731732
}
732733
}
@@ -1254,17 +1255,27 @@ public function validate($data): bool
12541255
$data = (array) $data;
12551256
}
12561257

1258+
$rules = $this->validationRules;
1259+
$rules = $this->cleanValidationRules($rules, $data);
1260+
1261+
// If no data existed that needs validation
1262+
// our job is done here.
1263+
if (empty($rules))
1264+
{
1265+
return true;
1266+
}
1267+
12571268
// ValidationRules can be either a string, which is the group name,
12581269
// or an array of rules.
1259-
if (is_string($this->validationRules))
1270+
if (is_string($rules))
12601271
{
1261-
$valid = $this->validation->run($data, $this->validationRules, $this->DBGroup);
1272+
$valid = $this->validation->run($data, $rules, $this->DBGroup);
12621273
}
12631274
else
12641275
{
12651276
// Replace any placeholders (i.e. {id}) in the rules with
12661277
// the value found in $data, if exists.
1267-
$rules = $this->fillPlaceholders($this->validationRules, $data);
1278+
$rules = $this->fillPlaceholders($rules, $data);
12681279

12691280
$this->validation->setRules($rules, $this->validationMessages);
12701281
$valid = $this->validation->run($data, null, $this->DBGroup);
@@ -1275,6 +1286,33 @@ public function validate($data): bool
12751286

12761287
//--------------------------------------------------------------------
12771288

1289+
/**
1290+
* Removes any rules that apply to fields that have not been set
1291+
* currently so that rules don't block updating when only updating
1292+
* a partial row.
1293+
*
1294+
* @param array $rules
1295+
*
1296+
* @return array
1297+
*/
1298+
protected function cleanValidationRules(array $rules, array $data = null)
1299+
{
1300+
if (empty($data))
1301+
{
1302+
return [];
1303+
}
1304+
1305+
foreach ($rules as $field => $rule)
1306+
{
1307+
if (! array_key_exists($field, $data))
1308+
{
1309+
unset($rules[$field]);
1310+
}
1311+
}
1312+
1313+
return $rules;
1314+
}
1315+
12781316
/**
12791317
* Replace any placeholders within the rules with the values that
12801318
* match the 'key' of any properties being set. For example, if

0 commit comments

Comments
 (0)