php - Laravel 4 reCaptcha validation -


i trying implement recaptcha in contact form controller in laravel 4.

i've saw validator::extend method, cannot accomplish custom validation.

here current code:

class contactcontroller extends basecontroller {      protected $captcha;      public function __construct()     {         $this->captcha = new captcha\captcha();         $this->captcha->setpublickey('xxx');         $this->captcha->setprivatekey('xxx');     }      public function postindex()     {         $response = $this->captcha->check();          validator::extend('recaptcha', function($attribute, $value, $parameters) {             return $response->isvalid();         });          $validator = validator::make(             input::all(),             array(                 array('recaptcha_response_field'    => 'recaptcha'),                 array('name'                        => 'required'),                 array('email'                       => 'required|email'),                 array('message'                     => 'required')             ),             array(                 'recaptcha' => 'incorrect captcha code'             )         );          if ( $validator->passes() )             return redirect::to('contact')->with('success', 'success');         else             return redirect::to('contact')->witherrors($validator)->withinput();     }  } 

i using recaptcha lib

if need fast set custom filter, , go.

route::filter('recaptcha', function() {         //lib file         require_once(app_path().'/recaptchalib.php');         //private , public keys         $config  = include app_path().'/config/recaptacha.php';         $privatekey = $config['privatekey'];         $resp = recaptcha_check_answer($privatekey, $_server["remote_addr"],                  $_post["recaptcha_challenge_field"], $_post["recaptcha_response_field"]);          if (!$resp->is_valid) {             // happens when captcha entered incorrectly             //here can throw exception or              die("the recaptcha wasn't entered correctly. go , try again." .                     "(recaptcha said: " . $resp->error . ")");         }     }); 

then add captcha in forms , that's that, in example using restful controllers so, added new rule in controller looks this

$this->beforefilter('recaptcha', array("only" => array(     'postcreate', ))); 

at least task don't see need of using else's libraries, since google documentation clear, in 5 different ways, 1 described above simplest , fastest one, asynchronously easy, read docs. https://developers.google.com/recaptcha/docs/php.

good luck :)


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -