CodeIgniter Forums
How to get old validation errors - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: How to get old validation errors (/thread-75617.html)



How to get old validation errors - falko - 02-26-2020

Hello!

I know that is possible to use the function old() to get items from a redirect()->withInput().

My doubt is: How to get old validation errors? Has a function for that?

PHP Code:
public function withInput()
    {
        
$session $this->ensureSession();

        
$input = [
            
'get'  => $_GET ?? [],
            
'post' => $_POST ?? [],
        ];

        
$session->setFlashdata('_ci_old_input'$input);

        
// If the validator has any errors, transmit those back
        // so they can be displayed when the validation is
        // handled within a method different than displaying the form.
        
$validator Services::validation();
        if (! empty(
$validator->getErrors()))
        {
            
$session->setFlashdata('_ci_validation_errors'serialize($validator->getErrors()));
        }

        return 
$this;
    } 


Thank you very much!


RE: How to get old validation errors - MGatner - 03-01-2020

You should pass the error messages during the redirect, using "with($name, $content)", e.g.: https://codeigniter4.github.io/userguide/general/common_functions.html#redirect


RE: How to get old validation errors - falko - 03-05-2020

(03-01-2020, 08:54 AM)MGatner Wrote: You should pass the error messages during the redirect, using "with($name, $content)", e.g.: https://codeigniter4.github.io/userguide/general/common_functions.html#redirect

Ok. Thank you! But why they are saved as flash data?

PHP Code:
$validator Services::validation();
if (! empty(
$validator->getErrors()))
{
    $session->setFlashdata('_ci_validation_errors'serialize($validator->getErrors()));




RE: How to get old validation errors - falko - 03-08-2020

Ok. I found it:

system/Validation/Validation.php:643

PHP Code:
public function getErrors(): array
{
    
// If we already have errors, we'll use those.
    // If we don't, check the session to see if any were
    // passed along from a redirect_with_input request.
    
if (empty($this->errors) && ! is_cli())
    {
        if (isset(
$_SESSION$_SESSION['_ci_validation_errors']))
        {
            
$this->errors unserialize($_SESSION['_ci_validation_errors']);
        }
    }

    return 
$this->errors ?? [];


It only works if session is active. Should not auto start the session here?

Thank you!