Symfony: 1.4.13
sfFormExtraPlugin
sfDoctrineGuardPlugin-5.0.0.tgz (VERSION: 1.0.4)"portal" to mój frontend.
Ustawiam w
apps\portal\config\app.yml :
Kod
all:
recaptcha:
active: true
publickey: "key"
privatekey: "key"
(kody kluczy oczywiście z :
https://www.google.com/recaptcha/admin/create)
Rozszerzam formularz od rejestracji dla: sfDoctrineGuardPlugin
lib\form\doctrine\sfDoctrineGuardPlugin\sfGuardRegisterForm.class.php/**
* sfGuardRegisterForm for registering new users
*
* @package Symfony
* @subpackage form
* @author Paweł Szczepanek <pauluz@pauluz.pl>
* @version SVN: $Id: BasesfGuardChangeUserPasswordForm.class.php 23536 2009-11-02 21:41:21Z Kris.Wallsmith $
*/
class sfGuardRegisterForm extends BasesfGuardRegisterForm
{
/**
* @see sfForm
*/
public function configure()
{
if (sfConfig::get('app_recaptcha_active', false))
{
$this->setWidget('captcha', new sfWidgetFormReCaptcha
(array( 'public_key' => sfConfig::get('app_recaptcha_publickey'),
'theme' => 'red\',
custom_translations : {
instructions_visual : "Wpisz oba słowa (ze spacją):",
instructions_audio : "Wpisz to co usłyszysz:",
play_again : "Odegraj dźwięk jeszcze raz",
cant_hear_this : "Pobierz dźwięk jako MP3",
visual_challenge : "Tryb wizualny",
audio_challenge : "Tryb audio",
refresh_btn : "Odśwież Captche",
help_btn : "Okienko pomocy",
incorrect_try_again : "Błąd. Spróbuj jeszcze raz"
},
nothing : \'null',
'culture' => 'pl',
), array('required'=> false)));
$this->setValidator('captcha', new sfValidatorReCaptcha
(array( 'private_key' => sfConfig::get('app_recaptcha_privatekey')
'captcha' => 'Źle podana Captcha (%error%).',
'server_problem' => 'Problemy z połączeniem do serwera (%error%).',
))
);
}
}
}
Nieco w hardkorowy sposób dostawiam do javascript zmienną 'custom_translations' aby przetłumaczyć reCaptche.
Zwróć uwagę na odpowiednie powstawianie tam znaków maskujących cudzysłowia.
Tworze moduł z tylko jednym plikiem: actions.class.php
apps\portal\modules\sfGuardRegister\actions\actions.class.phpKtóry zawiera:
class sfGuardRegisterActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
if ($this->getUser()->isAuthenticated())
{
$this->getUser()->setFlash('notice', 'You are already registered and signed in!');
$this->redirect('@homepage');
}
$this->form = new sfGuardRegisterForm();
if ($request->isMethod('post'))
{
'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
'recaptcha_response_field' => $request->getParameter('recaptcha_response_field'),
);
$this->form->bind(array_merge($request->getParameter($this->form->getName()), array('captcha' => $captcha)));
if ($this->form->isValid())
{
$this->form->offsetUnset('captcha');
$user = $this->form->save();
$this->getUser()->signIn($user);
$this->redirect('@homepage');
}
}
}
Jest to skopiowany plik z :
plugins\sfDoctrineGuardPlugin\modules\sfGuardRegister\lib\BasesfGuardRegisterActions.class.php- dopisałem do formularza 'captcha'.
(drobne wyjaśnienie tego myku jest w : plugins\sfFormExtraPlugin\lib\widget\sfWidgetFormReCaptcha.class.php)
Po wyczyszczeniu cache formularz od rejestracji powinien sprawdzać Captche.
U siebie miałem jeszcze ciekawie ostylowane formularze ale to już nie wpływa myślę na działanie.