• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation method to detect redirect->withInput() from a failed validation->run()

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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