Added ability to store captcha value in session between page redirects

This commit is contained in:
Wojciech Sznapka 2011-11-28 00:51:53 +01:00
parent 99a6b9d8b1
commit e92b657796
2 changed files with 12 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,16 +149,18 @@ class CaptchaType extends AbstractType
private function generateCaptchaValue() private function generateCaptchaValue()
{ {
$charset = 'abcdefghijklmnopqrstuvwxyz0123456789'; $value = $this->session->get($this->key);
$value = ''; if (null == $value || 0 == strlen($value)) {
$chars = str_split($charset); $charset = 'abcdefhjkmnprstuvwxyz23456789';
$chars = str_split($charset);
for ($i=0; $i<$this->length; $i++) { for ($i=0; $i<$this->length; $i++) {
$value.= $chars[array_rand($chars)]; $value.= $chars[array_rand($chars)];
}
$this->session->set($this->key, $value);
} }
$this->session->set($this->key, $value);
return $value; return $value;
} }
} }

View File

@ -38,9 +38,9 @@ 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);
} }
/** /**