Merge pull request #6 from wowo/keeping_captcha_for_refreshes

Added ability to store captcha value in session between page redirects
This commit is contained in:
Grégoire Passault 2011-11-29 02:13:43 -08:00
commit 5084b61fd9
2 changed files with 14 additions and 9 deletions

View File

@ -94,6 +94,7 @@ class CaptchaType extends AbstractType
public function buildForm(FormBuilder $builder, array $options) public function buildForm(FormBuilder $builder, array $options)
{ {
$this->key = $builder->getForm()->getName();
$builder->addValidator( $builder->addValidator(
new CaptchaValidator($this->session, $this->key) new CaptchaValidator($this->session, $this->key)
); );
@ -148,8 +149,9 @@ class CaptchaType extends AbstractType
private function generateCaptchaValue() private function generateCaptchaValue()
{ {
$charset = 'abcdefghijklmnopqrstuvwxyz0123456789'; if (!$this->session->has($this->key)) {
$value = ''; $value = '';
$charset = 'abcdefhjkmnprstuvwxyz23456789';
$chars = str_split($charset); $chars = str_split($charset);
for ($i=0; $i<$this->length; $i++) { for ($i=0; $i<$this->length; $i++) {
@ -157,6 +159,9 @@ class CaptchaType extends AbstractType
} }
$this->session->set($this->key, $value); $this->session->set($this->key, $value);
} else {
$value = $this->session->get($this->key);
}
return $value; return $value;
} }

View File

@ -38,10 +38,10 @@ class CaptchaValidator implements FormValidatorInterface
if (!($code && $excepted_code && is_string($code) && is_string($excepted_code) if (!($code && $excepted_code && is_string($code) && is_string($excepted_code)
&& $this->niceize($code) == $this->niceize($excepted_code))) { && $this->niceize($code) == $this->niceize($excepted_code))) {
$form->addError(new FormError('Bad code value')); $form->addError(new FormError('Bad code value'));
} } else {
$this->session->remove($this->key); $this->session->remove($this->key);
} }
}
/** /**
* Retrieve the excepted CAPTCHA code * Retrieve the excepted CAPTCHA code