Welcome Guest, Not a member yet? Register   Sign In
Validation method to detect redirect->withInput() from a failed validation->run()
#1

(This post was last modified: 09-13-2019, 08:54 AM by dave friend.)

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;
    
Reply


Messages In This Thread
Validation method to detect redirect->withInput() from a failed validation->run() - by dave friend - 09-12-2019, 03:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB