-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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 returns incorrect errors after Redirect with Input #5839
Comments
@MGatner I don't understand your use case.
|
The bug reproduces if a session is started in the request handler method, but not in the form display method. $routes->get('/', 'Home::index');
$routes->post('/', 'Home::post'); public function index()
{
return '<form method="post" action="/">
<input type="text" name="user">
<input type="submit">
</form>';
//return view('welcome_message');
}
public function post()
{
session();
$rules = [
'user' => ['label' => 'User', 'rules' => 'required']
];
if (!$this->validate($rules)) {
return redirect()->to('/')->withInput();
}
return 'success';
} If there was a validation error in the first attempt, then the second validation attempt, even if there were no errors, will cause a false positive. |
@iRedds Thanks! I confirmed the behavior.
|
The following code fixes the issue. public function index()
{
session();
return '<form method="post" action="/">
<input type="text" name="user">
<input type="submit">
</form>';
} |
For a workaround I went with |
Yes, this is a special case.
class ErrorBag extends MessageBag
{
protected $messages = [];
public function __construct()
{
$this->messages = session('key_for_errors') ?? [];
}
public function get(string $key) {}
public function set(string $key, $value) {}
public function has(string $key): bool {}
public function hasErrors(): bool {}
public function clear(): self {}
public function all(): array {}
}
function errorBag()
{
return new ErrorBag();
} Validation::run() $this->errors = errorBag()->clear();
//.... validation
return $this->errors->hasErrors(); To receive errors, the developer does not need to manually start the session and initialize the validation class. |
While i have the opportunity, i want to express my opinion. It seems to me that saving errors in the session in the withInput() method does not match the method name. |
@iRedds Agree with these. It seems worth considering. |
It should be |
It is a strange conflation and part of the reason it took me so long to track down the underlying issue. |
PHP Version
7.4
CodeIgniter4 Version
4.1.9
CodeIgniter4 Installation Method
Composer (using
codeigniter4/appstarter
)Which operating systems have you tested for this bug?
Linux
Which server did you use?
apache
Database
MySQL
What happened?
Validation fails incorrectly following a redirect that uses
_ci_validation_errors
.Steps to Reproduce
return redirect()->...->withInput()
This is due to
getErrors()
filling in the flashdata errors from_ci_validation_errors
because$this->errors
is empty:CodeIgniter4/system/Validation/Validation.php
Lines 672 to 677 in 94cc0fe
Probably need to differentiate
null
but I can't dig into the implications of this right now.Expected Output
The new Validation should pass regardless of the redirected errors.
Anything else?
No response
The text was updated successfully, but these errors were encountered: