diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index ffada26..0bea461 100755 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -94,6 +94,7 @@ class CaptchaType extends AbstractType public function buildForm(FormBuilder $builder, array $options) { + $this->key = $builder->getForm()->getName(); $builder->addValidator( new CaptchaValidator($this->session, $this->key) ); @@ -148,16 +149,20 @@ class CaptchaType extends AbstractType private function generateCaptchaValue() { - $charset = 'abcdefghijklmnopqrstuvwxyz0123456789'; - $value = ''; - $chars = str_split($charset); + if (!$this->session->has($this->key)) { + $value = ''; + $charset = 'abcdefhjkmnprstuvwxyz23456789'; + $chars = str_split($charset); - for ($i=0; $i<$this->length; $i++) { - $value.= $chars[array_rand($chars)]; + for ($i=0; $i<$this->length; $i++) { + $value.= $chars[array_rand($chars)]; + } + + $this->session->set($this->key, $value); + } else { + $value = $this->session->get($this->key); } - $this->session->set($this->key, $value); - return $value; } } diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php index 47945c1..49707c9 100644 --- a/Validator/CaptchaValidator.php +++ b/Validator/CaptchaValidator.php @@ -38,9 +38,9 @@ class CaptchaValidator implements FormValidatorInterface if (!($code && $excepted_code && is_string($code) && is_string($excepted_code) && $this->niceize($code) == $this->niceize($excepted_code))) { $form->addError(new FormError('Bad code value')); + } else { + $this->session->remove($this->key); } - - $this->session->remove($this->key); } /**