Skip to content

Commit

Permalink
Merge pull request #1763 from codeigniter4/modelvalidate
Browse files Browse the repository at this point in the history
Ensure validation works in Model with errors as part of rules. Fixes #1574
  • Loading branch information
lonnieezell authored Feb 27, 2019
2 parents f746fa0 + 513ce9c commit 9553ead
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 14 deletions.
16 changes: 11 additions & 5 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ public function save($data)
* properties as an array suitable for use in creates and updates.
*
* @param string|object $data
* @param string|null $primaryKey
* @param string $dateFormat
* @param string|null $primaryKey
* @param string $dateFormat
*
* @return array
* @throws \ReflectionException
Expand Down Expand Up @@ -572,13 +572,12 @@ public function getInsertID()

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


/**
* Inserts data into the current table. If an object is provided,
* it will attempt to convert it to an array.
*
* @param array|object $data
* @param boolean $returnID Whether insert ID should be returned or not.
* @param boolean $returnID Whether insert ID should be returned or not.
*
* @return integer|string|boolean
* @throws \ReflectionException
Expand Down Expand Up @@ -699,7 +698,7 @@ public function insertBatch($set = null, $escape = null, $batchSize = 100, $test
* it will attempt to convert it into an array.
*
* @param integer|array|string $id
* @param array|object $data
* @param array|object $data
*
* @return boolean
* @throws \ReflectionException
Expand Down Expand Up @@ -1362,6 +1361,13 @@ protected function fillPlaceholders(array $rules, array $data)
{
foreach ($rule as &$row)
{
// Should only be an `errors` array
// which doesn't take placeholders.
if (is_array($row))
{
continue;
}

$row = strtr($row, $replacements);
}
continue;
Expand Down
17 changes: 8 additions & 9 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,22 +373,21 @@ public function setRule(string $field, string $label = null, string $rules, arra
*/
public function setRules(array $rules, array $errors = []): ValidationInterface
{
$this->rules = $rules;
$this->customErrors = $errors;

if (empty($errors))
foreach ($rules as $field => &$rule)
{
foreach ($rules as $field => $setup)
if (is_array($rule))
{
if (isset($setup['errors']))
if (array_key_exists('errors', $rule))
{
$this->customErrors[$field] = $setup['errors'];
$this->customErrors[$field] = $rule['errors'];
unset($rule['errors']);
}
}
}
else
{
$this->customErrors = $errors;
}

$this->rules = $rules;

return $this;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/_support/Models/ValidErrorsModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php namespace Tests\Support\Models;

use CodeIgniter\Model;

class ValidErrorsModel extends Model
{
protected $table = 'job';

protected $returnType = 'object';

protected $useSoftDeletes = false;

protected $dateFormat = 'integer';

protected $allowedFields = [
'name',
'description',
];

protected $validationRules = [
'name' => [
'required',
'min_length[10]',
'errors' => [
'min_length' => 'Minimum Length Error',
]
],
'token' => 'in_list[{id}]',
];
}
24 changes: 24 additions & 0 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Tests\Support\Models\SecondaryModel;
use Tests\Support\Models\SimpleEntity;
use Tests\Support\Models\UserModel;
use Tests\Support\Models\ValidErrorsModel;
use Tests\Support\Models\ValidModel;

/**
Expand Down Expand Up @@ -1031,4 +1032,27 @@ public function testRequiredWithValidationTrue()

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

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1574
*/
public function testValidationIncludingErrors()
{
$model = new ValidErrorsModel($this->db);

$data = [
'description' => 'This is a first test!',
'name' => 'valid',
'id' => 42,
'token' => 42,
];

$id = $model->insert($data);

$this->assertFalse((bool)$id);

$errors = $model->errors();

$this->assertEquals('Minimum Length Error', $model->errors()['name']);
}
}
19 changes: 19 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,25 @@ public function rulesSetupProvider()

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

public function testSetRulesRemovesErrorsArray()
{
$rules = [
'foo' => [
'label' => 'Foo Bar',
'rules' => 'min_length[10]',
'errors' => [
'min_length' => 'The {field} field is very short.',
],
],
];

$this->validation->setRules($rules, []);

$this->validation->run(['foo' => 'abc']);

$this->assertEquals('The Foo Bar field is very short.', $this->validation->getError('foo'));
}

public function testInvalidRule()
{
$this->expectException(ValidationException::class);
Expand Down

0 comments on commit 9553ead

Please sign in to comment.