How to add a captcha field in Symfony 1.1
Step by step on how to add a captcha field to a Symfony 1.1 form.
1. Install sfFormExtraPlugin:
$ symfony plugin:install sfFormExtraPlugin
$ symfony cache:clear
2. Register for public and private keys at recaptcha.net. It’s free! Note that when you’re creating keys, it’s better to tick the ‘Enable this key on all domains (global key)’. This will allow you to use it anywhere. But of course it would be a good idea to untick this once your website goes live.
3. Add the captcha widget and validator to your form class:
// widget:
‘captcha’ => new sfWidgetFormReCaptcha(array(‘public_key’ => ‘your public key here’))
// validator:
‘captcha’ => new sfValidatorReCaptcha(array(‘private_key’ => ‘your private key here’))
4. And lastly, the way you bind the form values is quite different now. Instead of:
$form->bind($values);
with the captcha, now you have to do:
$captcha = array(
‘recaptcha_challenge_field’ => $request->getParameter(‘recaptcha_challenge_field’),
‘recaptcha_response_field’ => $request->getParameter(‘recaptcha_response_field’),
);
$form->bind(array_merge($values, array(‘captcha’ => $captcha)));
That’s it! :-)
These information are kind of available in sfWidgetFormReCaptcha.class.php and sfValidatorReCaptcha.class.php, so I would recommend go and look there too.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
This doesn’t work for me at all. I am using symfony 1.3. Could you please explain what is $values in part 4.
@jayz
Hi Jayz,
First of all, this is a guide for Symfony 1.1, not 1.3. I have not tested it on other versions of Symfony. So I don’t know if it’s working on 1.3.
Secondly, this is not a guide for Admin/CRUD generated forms. This is a guide for those who build their forms manually using sfForm.
But to answer your question:
$values is the tainted values, the values from the request object.
For example:
$values = $request->getParameters(‘user’);
Merry Christmas and happy coding :)