Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:bcit-ci/CodeIgniter4 into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Jan 25, 2019
2 parents bea1dd5 + 4c57656 commit 415b805
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 61 deletions.
44 changes: 41 additions & 3 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ public function update($id = null, $data = null)
{
if ($this->validate($data) === false)
{
dd($this->errors());
return false;
}
}
Expand Down Expand Up @@ -1254,17 +1255,27 @@ public function validate($data): bool
$data = (array) $data;
}

$rules = $this->validationRules;
$rules = $this->cleanValidationRules($rules, $data);

// If no data existed that needs validation
// our job is done here.
if (empty($rules))
{
return true;
}

// ValidationRules can be either a string, which is the group name,
// or an array of rules.
if (is_string($this->validationRules))
if (is_string($rules))
{
$valid = $this->validation->run($data, $this->validationRules, $this->DBGroup);
$valid = $this->validation->run($data, $rules, $this->DBGroup);
}
else
{
// Replace any placeholders (i.e. {id}) in the rules with
// the value found in $data, if exists.
$rules = $this->fillPlaceholders($this->validationRules, $data);
$rules = $this->fillPlaceholders($rules, $data);

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

//--------------------------------------------------------------------

/**
* Removes any rules that apply to fields that have not been set
* currently so that rules don't block updating when only updating
* a partial row.
*
* @param array $rules
*
* @return array
*/
protected function cleanValidationRules(array $rules, array $data = null)
{
if (empty($data))
{
return [];
}

foreach ($rules as $field => $rule)
{
if (! array_key_exists($field, $data))
{
unset($rules[$field]);
}
}

return $rules;
}

/**
* Replace any placeholders within the rules with the values that
* match the 'key' of any properties being set. For example, if
Expand Down
Loading

0 comments on commit 415b805

Please sign in to comment.