Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make validation placeholders always available #2897

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1432,10 +1432,6 @@ public function validate($data): bool
return true;
}

// Replace any placeholders (i.e. {id}) in the rules with
// the value found in $data, if exists.
$rules = $this->fillPlaceholders($rules, $data);

$this->validation->setRules($rules, $this->validationMessages);
$valid = $this->validation->run($data, null, $this->DBGroup);

Expand Down Expand Up @@ -1488,6 +1484,10 @@ protected function cleanValidationRules(array $rules, array $data = null): array
*
* 'required|is_unique[users,email,id,13]'
*
* @codeCoverageIgnore
*
* @deprecated use fillPlaceholders($rules, $data) from Validation instead
*
* @param array $rules
* @param array $data
*
Expand Down
62 changes: 62 additions & 0 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public function run(array $data = null, string $group = null, string $db_group =
return false;
}

// Replace any placeholders (i.e. {id}) in the rules with
// the value found in $data, if exists.
$this->rules = $this->fillPlaceholders($this->rules, $data);

// Need this for searching arrays in validation.
helper('array');

Expand Down Expand Up @@ -603,6 +607,64 @@ public function loadRuleGroup(string $group = null)
return $this->rules;
}

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

/**
* Replace any placeholders within the rules with the values that
* match the 'key' of any properties being set. For example, if
* we had the following $data array:
*
* [ 'id' => 13 ]
*
* and the following rule:
*
* 'required|is_unique[users,email,id,{id}]'
*
* The value of {id} would be replaced with the actual id in the form data:
*
* 'required|is_unique[users,email,id,13]'
*
* @param array $rules
* @param array $data
*
* @return array
*/
protected function fillPlaceholders(array $rules, array $data): array
{
$replacements = [];

foreach ($data as $key => $value)
{
$replacements["{{$key}}"] = $value;
}

if (! empty($replacements))
{
foreach ($rules as &$rule)
{
if (is_array($rule))
{
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;
}

$rule = strtr($rule, $replacements);
}
}

return $rules;
}

//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Errors
Expand Down
31 changes: 31 additions & 0 deletions tests/system/Validation/UniqueRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,35 @@ public function testIsUniqueIgnoresParams()
}

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

/**
* @group DatabaseLive
*/
public function testIsUniqueIgnoresParamsPlaceholders()
{
$this->hasInDatabase('user', [
'name' => 'Derek',
'email' => '[email protected]',
'country' => 'GB',
]);

$db = Database::connect();
$row = $db->table('user')
->limit(1)
->get()
->getRow();

$data = [
'id' => $row->id,
'email' => '[email protected]',
];

$this->validation->setRules([
'email' => "is_unique[user.email,id,{id}]",
]);

$this->assertTrue($this->validation->run($data));
}

//--------------------------------------------------------------------
}
Loading