Skip to content

Commit

Permalink
Merge pull request #2433 from codeigniter4/validinserts
Browse files Browse the repository at this point in the history
Inserting through a model should respect all validation rules. Fixes #2384
  • Loading branch information
lonnieezell authored Dec 4, 2019
2 parents 9765931 + 1e61853 commit 2b8c751
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
39 changes: 33 additions & 6 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ class Model
*/
protected $skipValidation = false;

/**
* Whether rules should be removed that do not exist
* in the passed in data. Used between inserts/updates.
*
* @var boolean
*/
protected $cleanValidationRules = true;

/**
* Our validator instance.
*
Expand Down Expand Up @@ -682,7 +690,7 @@ public function insert($data = null, bool $returnID = true)
// Validate data before saving.
if ($this->skipValidation === false)
{
if ($this->validate($data) === false)
if ($this->cleanRules(false)->validate($data) === false)
{
return false;
}
Expand Down Expand Up @@ -750,7 +758,7 @@ public function insertBatch(array $set = null, bool $escape = null, int $batchSi
{
foreach ($set as $row)
{
if ($this->validate($row) === false)
if ($this->cleanRules(false)->validate($row) === false)
{
return false;
}
Expand Down Expand Up @@ -812,7 +820,7 @@ public function update($id = null, $data = null): bool
// Validate data before saving.
if ($this->skipValidation === false)
{
if ($this->validate($data) === false)
if ($this->cleanRules(true)->validate($data) === false)
{
return false;
}
Expand Down Expand Up @@ -867,7 +875,7 @@ public function updateBatch(array $set = null, string $index = null, int $batchS
{
foreach ($set as $row)
{
if ($this->validate($row) === false)
if ($this->cleanRules(true)->validate($row) === false)
{
return false;
}
Expand Down Expand Up @@ -1005,7 +1013,7 @@ public function replace($data = null, bool $returnSQL = false)
// Validate data before saving.
if (! empty($data) && $this->skipValidation === false)
{
if ($this->validate($data) === false)
if ($this->cleanRules(true)->validate($data) === false)
{
return false;
}
Expand Down Expand Up @@ -1359,6 +1367,23 @@ public function setValidationMessage(string $field, array $fieldMessages)

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

/**
* Should validation rules be removed before saving?
* Most handy when doing updates.
*
* @param boolean $choice
*
* @return $this
*/
public function cleanRules(bool $choice = false)
{
$this->cleanValidationRules = $choice;

return $this;
}

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

/**
* Validate the data against the validation rules (or the validation group)
* specified in the class property, $validationRules.
Expand Down Expand Up @@ -1390,7 +1415,9 @@ public function validate($data): bool
$rules = $this->validation->loadRuleGroup($rules);
}

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

// If no data existed that needs validation
// our job is done here.
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Models/ValidModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ValidModel extends Model
'required',
'min_length[3]',
],
'token' => 'in_list[{id}]',
'token' => 'permit_empty|in_list[{id}]',
];

protected $validationMessages = [
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ public function testRequiredWithValidationTrue()

$data = [
'name' => 'foobar',
'description' => 'just becaues we have to',
'description' => 'just because we have to',
];

$this->assertTrue($model->insert($data) !== false);
Expand Down

0 comments on commit 2b8c751

Please sign in to comment.