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

Bug: Validation placeholder not being replaced #2817

Closed
nControl88 opened this issue Apr 9, 2020 · 2 comments
Closed

Bug: Validation placeholder not being replaced #2817

nControl88 opened this issue Apr 9, 2020 · 2 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@nControl88
Copy link

nControl88 commented Apr 9, 2020

Describe the bug
When using validation placeholders in a model and validating data, the placeholder is not being replaced with the actual data. The validation therefore returns false.

CodeIgniter 4 version
4.0.2

Expected behavior, and steps to reproduce if appropriate
Using the myth-auth package and the GroupModel. Validation rules are defined in the model as:

protected $validationRules = [
	'name' => 'required|max_length[255]|is_unique[auth_groups.name,name,{name}]',
	'description' => 'max_length[255]',
];

When trying to update a group in the controller using an already existing name in the database:

$groupModel = new GroupModel();

if (! $this->validate($rules = $groupModel->getValidationRules())) {
	return redirect()->back()->withInput()->with('errors', $groupModel->errors());
}

The validation returns false. Looking at the DB (MySQL) query ran it can be seen why:
SELECT 1 FROM `auth_groups` WHERE `name` = 'admin' AND `name` != '{name}' LIMIT 1

A manual replacement of {name} with the actual value resolves the issue.

@nControl88 nControl88 added the bug Verified issues on the current code behavior or pull requests that will fix them label Apr 9, 2020
@samsonasik
Copy link
Member

i guess you are using postgresql, seems similar issue with my comment at #2812 (comment)

@nControl88
Copy link
Author

After digging through the code I might have found the solution. This might also solve #2812.

In the Model class the validate() function includes a fillPlaceholders() before it sets the rules and runs the validation:

CodeIgniter4/system/Model.php

Lines 1434 to 1439 in 895f1e6

// 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);

In the Controller class this call to fillPlaceholders() is not present:

return $this->validator
->withRequest($this->request)
->setRules($rules, $messages)
->run();

When the fillPlaceholders() is added to the Controller class and called before the validator runs, the issue is solved:

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

$success = $this->validator
	->withRequest($this->request)
	->setRules($rules, $messages)
	->run();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

2 participants