fix multiple captcha on page

This commit is contained in:
Piotr Antosik 2014-07-21 14:46:14 +02:00
parent ce96122ef2
commit 458e7cafef
1 changed files with 6 additions and 13 deletions

View File

@ -26,12 +26,6 @@ class CaptchaType extends AbstractType
*/ */
protected $session; protected $session;
/**
* The session key
* @var string
*/
protected $key = null;
/** /**
* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator * @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator
*/ */
@ -67,12 +61,10 @@ class CaptchaType extends AbstractType
*/ */
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$this->key = 'gcb_'.$builder->getForm()->getName();
$validator = new CaptchaValidator( $validator = new CaptchaValidator(
$this->translator, $this->translator,
$this->session, $this->session,
$this->key, sprintf('gcp_%s', $builder->getForm()->getName()),
$options['invalid_message'], $options['invalid_message'],
$options['bypass_code'], $options['bypass_code'],
$options['humanity'] $options['humanity']
@ -94,21 +86,22 @@ class CaptchaType extends AbstractType
throw new \InvalidArgumentException('GregwarCaptcha: The reload option cannot be set without as_url, see the README for more information'); throw new \InvalidArgumentException('GregwarCaptcha: The reload option cannot be set without as_url, see the README for more information');
} }
$key = sprintf('gcp_%s', $form->getName());
if ($options['humanity'] > 0) { if ($options['humanity'] > 0) {
$humanityKey = $this->key.'_humanity'; $humanityKey = sprintf('%s_humanity', $key);
if ($this->session->get($humanityKey, 0) > 0) { if ($this->session->get($humanityKey, 0) > 0) {
$isHuman = true; $isHuman = true;
} }
} }
if ($options['as_url']) { if ($options['as_url']) {
$key = $this->key;
$keys = $this->session->get($options['whitelist_key'], array()); $keys = $this->session->get($options['whitelist_key'], array());
if (!in_array($key, $keys)) { if (!in_array($key, $keys)) {
$keys[] = $key; $keys[] = $key;
} }
$this->session->set($options['whitelist_key'], $keys); $this->session->set($options['whitelist_key'], $keys);
$options['session_key'] = $this->key; $options['session_key'] = $key;
} }
$view->vars = array_merge($view->vars, array( $view->vars = array_merge($view->vars, array(
@ -126,7 +119,7 @@ class CaptchaType extends AbstractType
$persistOptions[$key] = $options[$key]; $persistOptions[$key] = $options[$key];
} }
$this->session->set($this->key, $persistOptions); $this->session->set($key, $persistOptions);
} }
/** /**