CodeIgniter Forums
Validation method to detect redirect->withInput() from a failed validation->run() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Validation method to detect redirect->withInput() from a failed validation->run() (/showthread.php?tid=74341)



Validation method to detect redirect->withInput() from a failed validation->run() - dave friend - 09-12-2019

If you are processing HTML forms using the Post / Redirect / Get pattern (as everyone should - read this and this) it can be handy to know if we arrived at some point because field validation failed in a form's action method.

For instance, consider a form for updating an existing "user". If this is the "first time" to this form we will populate the fields from the database otherwise we repopulate using "old" data.

Here's one idea on how to implement what I'm asking for.

PHP Code:
    
    
/**
    * Indicates whether or not we're currently experiencing a redirect->withInput()
    * from a form's processing method where Validation::run() failed
    * 
    * @return boolean
    */
    public function isFailedCallback()
    {
        // Start up the session if it's not already
        if( ! isset($_SESSION))
        {
            session();
        }

        if(isset($_SESSION['_ci_old_input']))
        {
            $this->getErrors();  // so that the Validation::errors property is primed and ready for action
            return true;
        }
        
        
return false;