Moving translation logic to the validator (fixes for 2.2, see #52)

This commit is contained in:
Gregwar 2013-03-03 13:54:31 +01:00
parent 1fef229c8f
commit 4e13d2d4cb
3 changed files with 20 additions and 3 deletions

View File

@ -4,6 +4,7 @@ services:
arguments: arguments:
- @session - @session
- @gregwar_captcha.generator - @gregwar_captcha.generator
- @translator
- %gregwar_captcha.config% - %gregwar_captcha.config%
tags: tags:
- { name: form.type, alias: captcha } - { name: form.type, alias: captcha }

View File

@ -9,6 +9,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvents;
use Symfony\Component\Translation\TranslatorInterface;
use Gregwar\CaptchaBundle\Validator\CaptchaValidator; use Gregwar\CaptchaBundle\Validator\CaptchaValidator;
use Gregwar\CaptchaBundle\Generator\CaptchaGenerator; use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
@ -36,6 +37,11 @@ class CaptchaType extends AbstractType
*/ */
protected $generator; protected $generator;
/**
* @var TranslatorInterface
*/
protected $translator;
/** /**
* Options * Options
* @var array * @var array
@ -47,10 +53,11 @@ class CaptchaType extends AbstractType
* @param \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator * @param \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator
* @param array $options * @param array $options
*/ */
public function __construct(SessionInterface $session, CaptchaGenerator $generator, $options) public function __construct(SessionInterface $session, CaptchaGenerator $generator, TranslatorInterface $translator, $options)
{ {
$this->session = $session; $this->session = $session;
$this->generator = $generator; $this->generator = $generator;
$this->translator = $translator;
$this->options = $options; $this->options = $options;
} }
@ -63,6 +70,7 @@ class CaptchaType extends AbstractType
$this->key = 'gcb_'.$builder->getForm()->getName(); $this->key = 'gcb_'.$builder->getForm()->getName();
$validator = new CaptchaValidator( $validator = new CaptchaValidator(
$this->translator,
$this->session, $this->session,
$this->key, $this->key,
$options['invalid_message'], $options['invalid_message'],

View File

@ -5,6 +5,7 @@ namespace Gregwar\CaptchaBundle\Validator;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvent;
use Symfony\Component\Translation\TranslatorInterface;
/** /**
* Captcha validator * Captcha validator
@ -39,14 +40,21 @@ class CaptchaValidator
*/ */
private $humanity; private $humanity;
/**
* Translator
* @var TranslatorInterface
*/
private $translator;
/** /**
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param string $key * @param string $key
* @param string $invalidMessage * @param string $invalidMessage
* @param string|null $bypassCode * @param string|null $bypassCode
*/ */
public function __construct(SessionInterface $session, $key, $invalidMessage, $bypassCode, $humanity) public function __construct(TranslatorInterface $translator, SessionInterface $session, $key, $invalidMessage, $bypassCode, $humanity)
{ {
$this->translator = $translator;
$this->session = $session; $this->session = $session;
$this->key = $key; $this->key = $key;
$this->invalidMessage = $invalidMessage; $this->invalidMessage = $invalidMessage;
@ -73,7 +81,7 @@ class CaptchaValidator
} }
if (!($code && is_string($code) && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) { if (!($code && is_string($code) && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) {
$form->addError(new FormError($this->invalidMessage)); $form->addError(new FormError($this->translator->trans($this->invalidMessage, array(), 'validators')));
} else { } else {
if ($this->humanity > 0) { if ($this->humanity > 0) {
$this->updateHumanity($this->humanity); $this->updateHumanity($this->humanity);