Display validation errors without using Sessions (useful for Facebook-Tab apps)

Submitted by flei - 3 years ago

When showing a website through a Facebook Page tab validation errors are not displayed, because Facebook "forgets" the sessions due to the page proxy. One problem with this is that session data is destroyed / forgotten on each request going through that proxy. This applies to Redirect::back()->withErrors($validator) for showing the form with validation errors because sessions are used . To avoid this simply fiddle the error data together and pass it directly to the respective action that handles the input form. One thing to mention is that calling other controller actions directly of course does not affect the route. Let's say editing happens at "/edit" and the "registration complete" message is shown at "/complete". Calling the action within the controller the form with errors is now shown at "/complete" whereas "/edit" would be shown if you redirected back (Line 22). I personally don't really care too much about this since the "real" web address is not shown in the FB page tab anyway due to the page proxy. Looking forward to hearing your opinions on this. Maybe there is even a better way.

public $fb_state;   // true if shown through FB page iframe (to be set elsewhere)

public function postCompleteRegistration() {
 
        //
        // ... (define $validation_rules and $error_messages here)
        // 
 
    	$validator = Validator::make(Input::all(), $validation_rules, $error_messages);

	    if($validator->fails()) {
	    	
	    	// Hack for Facebook: Sessions get destroyed with every page call, 
            // thus Redirect::back()->withErrors() won't work
	    	if ($this->fb_state) {
	    		$view_errors = new Illuminate\Support\ViewErrorBag;
	    		$view_errors->put('default', $validator->getMessageBag());
                
                return $this->postRegister(array('errors'=> $view_errors, 'input'	=> Input::all()));
	    	}
	        
            return Redirect::back()->withErrors($validator)->withInput();
	    } 
        
}

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks