• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get old validation errors

#1
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!
Reply

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

#3
(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...l#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()));

Reply

#4
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!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.